일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 파이썬
- 시바견
- t1
- concurrency
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- kaggle
- 30. Substring with Concatenation of All Words
- attribute
- iterator
- 715. Range Module
- Regular Expression
- Class
- Generator
- 43. Multiply Strings
- Python Code
- shiba
- Substring with Concatenation of All Words
- LeetCode
- Python
- 프로그래머스
- 315. Count of Smaller Numbers After Self
- Python Implementation
- Decorator
- 컴퓨터의 구조
- data science
- 밴픽
- Convert Sorted List to Binary Search Tree
- DWG
- Today
- Total
목록LeetCode (205)
Scribbling
class Solution: def hammingWeight(self, n: int) -> int: ret = 0 for i in range(32): ret += n & 1 n >>= 1 return ret
class Solution: def titleToNumber(self, columnTitle: str) -> int: ret = 0 order = 1 for char in columnTitle[::-1]: ret += order * (ord(char) - 64) order *= 26 return ret
class Solution: def isPalindrome(self, s: str) -> bool: s = s.lower().strip() if not s: return True l, r = 0, len(s) - 1 while l < r: if not s[l].isalnum(): l += 1 continue if not s[r].isalnum(): r -= 1 continue if s[l] != s[r]: return False l += 1 r -= 1 return True
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: return self.isMirror(root.left, root.right) def isMirror(self, left, right): if left == None and right == None: return True if left == None or right == None: ret..
코드가 너무 복잡하다. # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': self.ret = None self.dfs(root, p, q) return self.ret def dfs(self, node, p, q): if node == None: return None l = self.dfs(node.left, p, q) if..
BST이기 때문에 Inorder-Traverse하면 된다. K번째 원소를 찾으면 순회를 중단한다. 때문에 O(K)이다. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.ret = 0 self.k = k self.traverse(root) return self.ret # returns whether traverse ..
어렵다... Greedy한 접근법이 필요하다. 내 머리론 못 풀었고, 아래 풀이를 바탕으로 코드를 작성했다. https://leetcode.com/problems/binary-tree-cameras/discuss/211180/JavaC%2B%2BPython-Greedy-DFS [Java/C++/Python] Greedy DFS - LeetCode Discuss Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 핵심은 아래와 같다. - Leaf Node에 카메라를 둘 이유가 전혀 없..
개인적으론 정말 정말 어렵다... 아래 선생님께서 매우 친절하게 잘 알려주신다... https://www.youtube.com/watch?v=LPFhl65R7ww&t=641s 핵심을 정리하면 아래와 같다. - nums1과 nums2 중 길이가 작은 리스트에 대해 binary search를 한다. - nums1과 nums2를 partition 하되, nums1의 left partition과 nums2의 left partition은 전체 길이의 절반 혹은 절반 + 1이 되도록 partition한다. 즉, nums1의 partition p1이 결정되면, p2도 자동으로 결정된다. p2 = (m + n + 1) // 2 - p1 - p1이 0 혹은 m이 되는 경우의 corner case를 처리한다. p1이 0이..
오늘 도둑은 binary tree를 털고 있다. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def rob(self, root: Optional[TreeNode]) -> int: a, b = self.dfs(root) return max(a, b) def dfs(self, node): if not node.left and not node.right: return 0, node.val if node.left and node.right..
O(N*logK) Time Complexity Solution using heap. class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: import heapq h = [] for num in nums: if len(h) < k: heapq.heappush(h, num) else: heapq.heappush(h, num) heapq.heappop(h) return h[0] A better approach is to quick selection, runs within O(N) time on average. But this runs with O(N) memory. import random class Solution: def fin..