알고리즘
-
[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..
-
[LeetCode] The K Weakest Rows in Matrix알고리즘 2022. 4. 23. 21:03
[LeetCode] The K Weakest Rows in Matrix 풀이_#1 두개의 요소를 기준으로 sort 하기. class Solution(object): def kWeakestRows(self, mat, k): """ :type mat: List[List[int]] :type k: int :rtype: List[int] """ n = len(mat) m = len(mat[0]) r = [] for i in range(n): r.append([mat[i].count(1), i]) r.sort(key=lambda x: (x[0],x[1])) return [r[_][1] for _ in range(k)]풀이_#2 숏코딩 class Solution(object): def kWeakestRows(sel..
-
[LeetCode] Middle of the Linked List알고리즘 2022. 4. 23. 20:36
[LeetCode] Middle of the Linked List 풀이_#1 fast 와 slow 사용 fast 는 2칸씩, slow 는 1칸씩 이동하므로 n 번의 반복뒤에는 fast 는 2n 칸, slow 는 n 칸에 있고 fast가 끝일경우(2n = len) slow(n = len/2) 는 중간에 위치하게됨. # 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 middleNode(self, head): """ :type head: ListNode :rtype..
-
[LeetCode] Fizz Buzz알고리즘 2022. 4. 23. 20:28
[LeetCode] Fizz Buzz 풀이_#1 반복문과 조건문 활용 class Solution(object): def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ r = []; for i in range(1, n+1): s = str(i); if (i % 3 == 0 and i % 5 == 0): s = "FizzBuzz" elif (i % 3 == 0): s = "Fizz" elif (i % 5 == 0): s = "Buzz" r.append(s) return r풀이_#2 리스트 이해 응용 class Solution(object): def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ ..