diff --git a/git_ci_push.sh b/git_ci_push.sh new file mode 100755 index 000000000..6cddd4f02 --- /dev/null +++ b/git_ci_push.sh @@ -0,0 +1 @@ +git aa && git ci -m 'upd' && git push \ No newline at end of file diff --git a/python/0040-combination-sum-ii.me.py b/python/0040-combination-sum-ii.me.py new file mode 100644 index 000000000..f6a6faa1c --- /dev/null +++ b/python/0040-combination-sum-ii.me.py @@ -0,0 +1,26 @@ +class Solution: + def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: + res = [] + candidates.sort() + + def dfs(i, cur, total): + if total == target: + res.append(cur.copy()) + return + if i >= len(candidates) or total > target: + return + + cur.append(candidates[i]) + + # i+1, because same number can not be chosen again + dfs(i+1, cur, total+candidates[i]) + cur.pop() + + # skip the same number + while i+1 < len(candidates) and candidates[i+1] == candidates[i]: + i += 1 + dfs(i+1, cur, total) + + dfs(0, [], 0) + + return res diff --git a/python/0046-permutations.me.py b/python/0046-permutations.me.py new file mode 100644 index 000000000..ce6fd3b99 --- /dev/null +++ b/python/0046-permutations.me.py @@ -0,0 +1,54 @@ +class Solution: + def recursive_neetcode_github(self, nums: List[int]) -> List[List[int]]: + res = [] + + # base case + if len(nums) == 1: + return [nums[:]] # nums[:] is a deep copy + + for i in range(len(nums)): + n = nums.pop(0) + perms = self.permute(nums) + + for perm in perms: + perm.append(n) + res.extend(perms) + nums.append(n) + return res + + def recursive_neetcode_youtube(self, nums: List[int]) -> List[List[int]]: + if len(nums) == 0: + return [[]] + + perms = self.permute(nums[1:]) + res = [] + + for p in perms: + for i in range(len(p) + 1): # may insert to tail: +1 + p_copy = p.copy() + p_copy.insert(i, nums[0]) + res.append(p_copy) + + return res + + def non_recursive(self, nums: List[int]) -> List[List[int]]: + perms = [[]] + + for n in nums: + new_perms = [] + for p in perms: + for i in range(len(p) + 1): + p_copy = p.copy() + p_copy.insert(i, n) + new_perms.append(p_copy) + perms = new_perms + + return perms + + + def permute(self, nums: List[int]) -> List[List[int]]: + # res = self.recursive_neetcode_github(nums) + # res = self.recursive_neetcode_youtube(nums) + res = self.non_recursive(nums) + + return res \ No newline at end of file diff --git a/python/0090-subsets-ii.me.py b/python/0090-subsets-ii.me.py new file mode 100644 index 000000000..4f0339f49 --- /dev/null +++ b/python/0090-subsets-ii.me.py @@ -0,0 +1,21 @@ +class Solution: + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: + res = [] + nums.sort() + subset = [] + + def dfs(i): + if i >= len(nums): + res.append(subset.copy()) + return + + subset.append(nums[i]) + dfs(i + 1) + subset.pop() + while i+1 < len(nums) and nums[i+1] == nums[i]: + i += 1 + dfs(i+1) + + dfs(0) + return res + diff --git a/python/0121-best-time-to-buy-and-sell-stock.py b/python/0121-best-time-to-buy-and-sell-stock.py index 401e39c55..f9f0698f8 100644 --- a/python/0121-best-time-to-buy-and-sell-stock.py +++ b/python/0121-best-time-to-buy-and-sell-stock.py @@ -8,3 +8,18 @@ def maxProfit(self, prices: List[int]) -> int: lowest = price res = max(res, price - lowest) return res + + +# 240813: chose not sell on the same day if it's the lowest day +def maxProfit_me(self, prices: List[int]) -> int: + lowest = prices[0] + res = 0 + + for price in prices: + lowest = min(price, lowest) + if price < lowest: + lowest = price + else: + res = max(res, price - lowest) + + return res \ No newline at end of file diff --git a/python/0199-binary-tree-right-side-view.me.py b/python/0199-binary-tree-right-side-view.me.py new file mode 100644 index 000000000..68221128b --- /dev/null +++ b/python/0199-binary-tree-right-side-view.me.py @@ -0,0 +1,29 @@ +# 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 rightSideView(self, root: Optional[TreeNode]) -> List[int]: + if not root: + return [] + + res = defaultdict(list) + q = deque() + q.append([root, 0]) + + while q: + node, level = q.popleft() + if node: + res[level].append(node.val) + q.append([node.left, level+1]) + q.append([node.right, level+1]) + + keys = list(res.keys()) + keys.sort() + answer = [] + for key in keys: + answer.append(res[key][-1]) + + return answer \ No newline at end of file diff --git a/python/0210-course-schedule-ii.py b/python/0210-course-schedule-ii.py index 8c59b6e1e..75fba5456 100644 --- a/python/0210-course-schedule-ii.py +++ b/python/0210-course-schedule-ii.py @@ -26,3 +26,14 @@ def dfs(crs): if dfs(c) == False: return [] return output + +# visit to track course that all prereq have been solved +# cycle is to introduced to detect the cycle +# dfs +# if already in cycle, return False +# then temporarily add it to cycle +# walk over each prereq, +# if all pass, remove from cycle +# if one prereq fails, +# it will never be removed from cycle +# nex time, dfs will fail immediately \ No newline at end of file diff --git a/python/0235-lowest-common-ancestor-of-a-binary-search-tree.me.py b/python/0235-lowest-common-ancestor-of-a-binary-search-tree.me.py new file mode 100644 index 000000000..7523fd2e3 --- /dev/null +++ b/python/0235-lowest-common-ancestor-of-a-binary-search-tree.me.py @@ -0,0 +1,19 @@ +# 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': + def dfs(node): + if node.val > p.val and node.val > q.val: + node = node.left + elif node.val < p.val and node.val < q.val: + node = node.right + else: + return node + return dfs(node) + + return dfs(root) \ No newline at end of file diff --git a/python/0286-walls-and-gates.me.py b/python/0286-walls-and-gates.me.py new file mode 100644 index 000000000..c9448ba4d --- /dev/null +++ b/python/0286-walls-and-gates.me.py @@ -0,0 +1,40 @@ +class Solution: + """ + @param rooms: m x n 2D grid + @return: nothing + """ + + def walls_and_gates(self, rooms: List[List[int]]): + ROWS, COLS = len(rooms), len(rooms[0]) + q = [] + visited = set() + + def visit_room(r, c, dist): + if ( + not r in range(ROWS) or + not c in range(COLS) or + (r, c) in visited or + rooms[r][c] == -1 + ): + return + + visited.add((r,c)) + q.append((r, c)) + rooms[r][c] = dist + + # get all gates + for r in range(ROWS): + for c in range(COLS): + if rooms[r][c] == 0: + q.append((r,c)) + visited.add((r, c)) + + dist = 0 + while q: + qlength = len(q) + dist += 1 + for _ in range(qlength): + r, c = q.pop(0) + for dr, dc in [[-1, 0], [1, 0], [0, -1], [0, 1]]: + # visit each adjacent + visit_room(r+dr, c+dc, dist) \ No newline at end of file
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: