분류 전체보기
-
[LeetCode] 189. Rotate Array알고리즘 2022. 4. 24. 17:18
풀이_#1 주기(cycle) 과 list slicing 을 활용한 풀이 class Solution(object): def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: None Do not return anything, modify nums in-place instead. """ n = len(nums) cycle = n move = k % cycle i = n - move s1 = nums[:i] s2 = nums[i:] s3 = s2 + s1 for _ in range(n): nums[_] = s3[_]import collections class Solution(object): def rotate(self, nums, k)..
-
[LeetCode] 977. Squares of a Sorted Array알고리즘 2022. 4. 24. 16:25
977. Squares of a Sorted Array 투포인터 풀이_#1 투포인터 활용 / 시간복잡도 O(N) 공간복잡도 O(1) (리턴값을 위한 리스트는 공간복잡도 계산에 포함되지 않음) class Solution(object): def sortedSquares(self, nums): """ :type nums: List[int] :rtype: List[int] """ n = len(nums) low = 0 high = n - 1 writeIdx = n - 1 r = [0] * n while(writeIdx >= 0): left = nums[low] ** 2 right = nums[high] ** 2 if (left
-
[LeetCode] # 35. Search Insert Position알고리즘 2022. 4. 24. 15:10
35. Search Insert Position 풀이_#1 이분탐색 사용 class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ low = 0 high = len(nums) - 1 while (low target: high = mid - 1 return low
-
[LeetCode] 278. First Bad Version알고리즘 2022. 4. 24. 15:01
https://leetcode.com/problems/first-bad-version/ [LeetCode] 278. First Bad Version 이분탐색을 활용한 풀이 # The isBadVersion API is already defined for you. # @param version, an integer # @return a bool # def isBadVersion(version): class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ low = 1 finish = n if (isBadVersion(1)): return 1 while (low
-
[LeetCode] 704. Binary Search알고리즘 2022. 4. 24. 14:16
[LeetCode] 704. Binary Search 풀이_#1 기본적인 이분탐색 활용 class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ def binarySearch(s,f,target,nums): if (s > f): return -1; mid = (s + f) // 2 if (target == nums[mid]): return mid elif (target > nums[mid]): return binarySearch(mid + 1, f, target, nums) else: return binarySearch(s, mid - 1, target, ..
-
[LeetCode] 234. Palindrome Linked List알고리즘 2022. 4. 24. 00:30
[LeetCode] 234. Palindrome Linked List 풀이_#1 공간복잡도 O(1), 시간복잡도 O(N) 의 풀이 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ slow, fast = head, head rev = None before = None while(fast and fast.next): fast = fast.ne..
-
[LeetCode] 1672. Richest Customer Wealth알고리즘 2022. 4. 24. 00:08
[LeetCode] 1672. Richest Customer Wealth 풀이_#1 class Solution(object): def maximumWealth(self, accounts): """ :type accounts: List[List[int]] :rtype: int """ n = [sum(accounts[i]) for i in range(len(accounts))] n.sort() return n[-1]풀이_#2 map 을 사용한 짧고 멋진 풀이 class Solution(object): def maximumWealth(self, accounts): """ :type accounts: List[List[int]] :rtype: int """ return max(map(sum, accounts))
-
[LeetCode] 1342. Number of Steps to Reduce a Number to Zero알고리즘 2022. 4. 23. 23:57
[LeetCode] 1342. Number of Steps to Reduce a Number to Zero 풀이_#1 if 문을 이용한 간단한 풀이 class Solution(object): def numberOfSteps(self, num): """ :type num: int :rtype: int """ r = 0 while (num): if (num % 2): num = num - 1 else: num /= 2 r += 1 return r비트를 이용한 풀이 Long answer The two operations in the problem are divide the number by 2 when even, or subtract 1 from the number when odd. In binary form..