diff --git a/README.md b/README.md index 8e05de7..28486a9 100755 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Please [BUY ME A COFFEE](https://www.buymeacoffee.com/chriswu) if you want to sh # Leetcode Problem Lists I found it makes sense to solve similar problems together, so that we can recognize the problem faster when we encounter a new one. My suggestion is to skip the HARD problems when you first go through these list. +* https://neetcode.io/practice (150 problems with video explaination) * https://www.programcreek.com/2013/08/leetcode-problem-classification/ * https://github.com/wisdompeak/LeetCode * https://docs.google.com/spreadsheets/d/1SbpY-04Cz8EWw3A_LBUmDEXKUMO31DBjfeMoA0dlfIA/edit#gid=126913158 ([huahua](https://www.youtube.com/user/xxfflower/videos)). diff --git a/problems/python3/alien-dictionary.py b/problems/python3/alien-dictionary.py new file mode 100644 index 0000000..23ca1ee --- /dev/null +++ b/problems/python3/alien-dictionary.py @@ -0,0 +1,36 @@ +class Solution: + def alienOrder(self, words: List[str]) -> str: + adj = collections.defaultdict(list) + inbounds = collections.Counter() + q = collections.deque() + ans = '' + + adj = {c: set() for word in words for c in word} + for i in range(len(words)-1): + w1, w2 = words[i], words[i+1] + minLen = min(len(w1), len(w2)) + if w1[:minLen]==w2[:minLen] and len(w1)>len(w2): return "" + + for j in range(minLen): + if w1[j]!=w2[j]: + adj[w1[j]].add(w2[j]) + break + + for c in adj: + for nc in list(adj[c]): + inbounds[nc] += 1 + + for c in adj: + if inbounds[c]==0: q.append(c) + + while q: + c = q.popleft() + + ans += c + + for nc in adj[c]: + inbounds[nc] -= 1 + if inbounds[nc]==0: q.append(nc) + + return ans if len(ans)==len(adj) else '' + diff --git a/problems/python3/best-time-to-buy-and-sell-stock-with-cooldown.py b/problems/python3/best-time-to-buy-and-sell-stock-with-cooldown.py new file mode 100644 index 0000000..06644e3 --- /dev/null +++ b/problems/python3/best-time-to-buy-and-sell-stock-with-cooldown.py @@ -0,0 +1,16 @@ +#dp[i][0] := max profit when last action is buy +#dp[i][1] := max profit when last action is sell +class Solution: + def maxProfit(self, prices: List[int]) -> int: + if not prices or len(prices)<=1: return 0 + + N = len(prices) + dp = [[0, 0] for _ in range(N)] + dp[0] = [-prices[0], 0] + dp[1][0] = max(-prices[1], -prices[0]) + dp[1][1] = max(prices[1]+dp[0][0], dp[0][1]) + + for i in range(2, N): + dp[i][0] = max(dp[i-2][1]-prices[i], dp[i-1][0]) + dp[i][1] = max(prices[i]+dp[i-1][0], dp[i-1][1]) + return max(dp[-1][0], dp[-1][1], 0) \ No newline at end of file diff --git a/problems/python3/burst-balloons.py b/problems/python3/burst-balloons.py new file mode 100644 index 0000000..aae31be --- /dev/null +++ b/problems/python3/burst-balloons.py @@ -0,0 +1,28 @@ +""" +dfs(l, r) return the max coins that we can get at range l to r. + +``` +for i in range(l, r+1): + dp[(l, r)] = max(dp[(l, r)], nums[l-1]*nums[i]*nums[r+1] + dfs(l, i-1) + dfs(i+1, r)) +``` +Assuming i is the last one we extract, the max coins we can get. + +Start from the last becasue, we are not able to track the neighbor if we start from the first we extract. + +Time: O(N^3) +Space: O(N^2) +""" +class Solution: + def maxCoins(self, nums: List[int]) -> int: + def dfs(l, r)->int: + if l>r: return 0 + if (l, r) in dp: return dp[(l, r)] + + dp[(l, r)] = 0 + for i in range(l, r+1): + dp[(l, r)] = max(dp[(l, r)], nums[l-1]*nums[i]*nums[r+1] + dfs(l, i-1) + dfs(i+1, r)) + return dp[(l, r)] + + nums = [1]+nums+[1] + dp = {} + return dfs(1, len(nums)-2) \ No newline at end of file diff --git a/problems/python3/cheapest-flights-within-k-stops.py b/problems/python3/cheapest-flights-within-k-stops.py new file mode 100644 index 0000000..4595802 --- /dev/null +++ b/problems/python3/cheapest-flights-within-k-stops.py @@ -0,0 +1,17 @@ +""" +Bellman-Ford. +Time: O(KE) +""" +class Solution: + def findCheapestPrice(self, N: int, flights: List[List[int]], src: int, dst: int, K: int) -> int: + prices = {n:float('inf') for n in range(N)} + prices[src] = 0 + + for k in range(K+1): + temp = prices.copy() + for source, destination, price in flights: + if prices[source]==float('inf'): continue + if prices[source]+price int: + dp = [0]*(N+1) + dp[0] = 1 + for i in range(len(dp)): + if i-1>=0: + dp[i] += dp[i-1] + if i-2>=0: + dp[i] += dp[i-2] + return dp[-1] \ No newline at end of file diff --git a/problems/python3/coin-change-ii.py b/problems/python3/coin-change-ii.py new file mode 100644 index 0000000..8b4ad26 --- /dev/null +++ b/problems/python3/coin-change-ii.py @@ -0,0 +1,12 @@ +class Solution: + def change(self, amount: int, coins: List[int]) -> int: + dp = [0]*(amount+1) + dp[0] = 1 + + coins.sort() + + for coin in coins: + for a in range(1, amount+1): + if a-coin<0: continue + dp[a] += dp[a-coin] + return dp[-1] \ No newline at end of file diff --git a/problems/python3/coin-change.py b/problems/python3/coin-change.py new file mode 100644 index 0000000..19571e4 --- /dev/null +++ b/problems/python3/coin-change.py @@ -0,0 +1,14 @@ +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [float('inf')]*(amount+1) + dp[0] = 0 + for coin in coins: + if coin List[int]: + ans = [0]*(n+1) + offset = 1 + + for i in range(1, n+1): + if offset*2==i: offset = offset*2 + ans[i] = 1+ans[i-offset] + return ans \ No newline at end of file diff --git a/problems/python3/decode-ways.py b/problems/python3/decode-ways.py new file mode 100644 index 0000000..3ac6c2d --- /dev/null +++ b/problems/python3/decode-ways.py @@ -0,0 +1,14 @@ +""" +dp[i] := up until s[:i] how many possibility? +""" +class Solution: + def numDecodings(self, s: str) -> int: + mapping = set([str(n) for n in range(1, 27)]) + N = len(s) + dp = [0]*(N+1) + dp[0] = 1 + + for i in range(1, N+1): + if i-1>=0 and s[i-1] in mapping: dp[i] += dp[i-1] + if i-2>=0 and s[i-2:i] in mapping: dp[i] += dp[i-2] + return dp[-1] \ No newline at end of file diff --git a/problems/python3/detect-squares.py b/problems/python3/detect-squares.py new file mode 100644 index 0000000..af86946 --- /dev/null +++ b/problems/python3/detect-squares.py @@ -0,0 +1,22 @@ +class DetectSquares: + + def __init__(self): + self.store = collections.Counter() + + def add(self, point: List[int]) -> None: + self.store[tuple(point)] += 1 + + def count(self, point: List[int]) -> int: + x, y = point + ans = 0 + + for dx, dy in self.store: + if abs(x-dx)!=abs(y-dy) or x==dx or y==dy: continue + ans += self.store[(dx, dy)]*self.store[(dx, y)]*self.store[(x, dy)] + return ans + + +# Your DetectSquares object will be instantiated and called as such: +# obj = DetectSquares() +# obj.add(point) +# param_2 = obj.count(point) \ No newline at end of file diff --git a/problems/python3/distinct-subsequences.py b/problems/python3/distinct-subsequences.py new file mode 100644 index 0000000..d84fcf6 --- /dev/null +++ b/problems/python3/distinct-subsequences.py @@ -0,0 +1,17 @@ +class Solution: + def numDistinct(self, s: str, t: str) -> int: + def dfs(i, j): + if (i, j) in visited: return visited[(i, j)] + + if j>=len(t): return 1 + if i>=len(s): return 0 + + if s[i]==t[j]: + visited[(i, j)] = dfs(i+1, j+1)+dfs(i+1, j) + else: + visited[(i, j)] = dfs(i+1, j) + return visited[(i, j)] + + visited = {} + dfs(0, 0) + return dfs(0, 0) \ No newline at end of file diff --git a/problems/python3/edit-distance.py b/problems/python3/edit-distance.py new file mode 100644 index 0000000..c8fa8f4 --- /dev/null +++ b/problems/python3/edit-distance.py @@ -0,0 +1,37 @@ +""" +dfs(i, j) +i being the unprocessed index in word1. +j, word2. + +MAIN LOGIC: +if word1[i]==word2[j], no operation need, return dfs(i+1, j+1) +if not, need 1 operation, so +replace: dfs(i+1, j+1) +insert: dfs(i, j+1) +delete: dfs(i+1, j) + +BASE CASE: +If both string are empty (i==N and j==M), no operation needed. +If one string are empty, then the remain operation is the length of the non-empty one. +""" +class Solution: + def minDistance(self, word1: str, word2: str) -> int: + N = len(word1) + M = len(word2) + def dfs(i, j)->int: + if i==N and j==M: return 0 + if i==N: return M-j + if j==M: return N-i + + if (i, j) in history: + return history[(i, j)] + + if word1[i]==word2[j]: + history[(i, j)] = dfs(i+1, j+1) + else: + history[(i, j)] = 1+min(dfs(i+1, j+1), dfs(i+1, j), dfs(i, j+1)) + + return history[(i, j)] + + history = {} + return dfs(0, 0) \ No newline at end of file diff --git a/problems/python3/happy-number.py b/problems/python3/happy-number.py new file mode 100644 index 0000000..7756197 --- /dev/null +++ b/problems/python3/happy-number.py @@ -0,0 +1,17 @@ +class Solution: + def isHappy(self, n: int) -> bool: + def digitSquare(n) -> int: + ans = 0 + while n>0: + ans += (n%10)**2 + n = n//10 + return ans + + visited = set() + visited.add(1) + + while n not in visited: + visited.add(n) + n = digitSquare(n) + + return n==1 diff --git a/problems/python3/house-robber-ii.py b/problems/python3/house-robber-ii.py new file mode 100644 index 0000000..1ed9b82 --- /dev/null +++ b/problems/python3/house-robber-ii.py @@ -0,0 +1,18 @@ +class Solution: + def rob(self, nums: List[int]) -> int: + if len(nums)<=1: return max(nums) + + N = len(nums) + dp = [[0, 0] for _ in range(N)] + dp[0][0] = nums[0] + + for i in range(1, N): + dp[i][0] = nums[i]+dp[i-1][1] + dp[i][1] = max(dp[i-1]) + + dp2 = [[0, 0] for _ in range(N)] + for i in range(1, N): + dp2[i][0] = nums[i]+dp2[i-1][1] + dp2[i][1] = max(dp2[i-1]) + + return max(dp[-1][1], dp2[-1][0]) \ No newline at end of file diff --git a/problems/python3/house-robber.py b/problems/python3/house-robber.py new file mode 100644 index 0000000..fbd86a5 --- /dev/null +++ b/problems/python3/house-robber.py @@ -0,0 +1,17 @@ +""" +Time: O(N) +Space: O(N), can reduece to O(1). + +dp[i][0] := max revenue if house i robbed +dp[i][1] := max revenue if house i not robbed +""" +class Solution: + def rob(self, nums: List[int]) -> int: + N = len(nums) + dp = [[0, 0] for _ in range(N)] + dp[0][0] = nums[0] + + for i in range(1, N): + dp[i][0] = nums[i]+dp[i-1][1] + dp[i][1] = max(dp[i-1]) + return max(dp[-1]) \ No newline at end of file diff --git a/problems/python3/interleaving-string.py b/problems/python3/interleaving-string.py new file mode 100644 index 0000000..76f9837 --- /dev/null +++ b/problems/python3/interleaving-string.py @@ -0,0 +1,13 @@ +class Solution: + def isInterleave(self, s1: str, s2: str, s3: str) -> bool: + def dfs(i, j): + if (i, j) in history: return False + if i+j==len(s3): return True + if i int: + #dp[i][j] := number of longest Common Subsequence with text2[:i] and text2[:j] + + N = len(text1) + M = len(text2) + + dp = [[0]*(M+1) for _ in range(N+1)] + + for i in range(1, N+1): + for j in range(1, M+1): + if text1[i-1]==text2[j-1]: + dp[i][j] = dp[i-1][j-1]+1 + else: + dp[i][j] = max(dp[i][j-1], dp[i-1][j]) + return dp[-1][-1] \ No newline at end of file diff --git a/problems/python3/longest-increasing-path-in-a-matrix.py b/problems/python3/longest-increasing-path-in-a-matrix.py new file mode 100644 index 0000000..99fed7d --- /dev/null +++ b/problems/python3/longest-increasing-path-in-a-matrix.py @@ -0,0 +1,26 @@ +""" +Time: O(MN) since the memo at most has MN index. +Space: O(MN) +""" +class Solution: + def longestIncreasingPath(self, matrix: List[List[int]]) -> int: + def dfs(i0, j0): + if (i0, j0) in memo: return memo[(i0, j0)] + ans = 1 + + for i, j in ((i0+1, j0), (i0-1, j0), (i0, j0+1),(i0, j0-1)): + if i<0 or i>=N or j<0 or j>=M: continue + if matrix[i][j]<=matrix[i0][j0]: continue + ans = max(ans, 1+dfs(i, j)) + + memo[(i0, j0)] = ans + return ans + + N = len(matrix) + M = len(matrix[0]) + memo = {} + ans = 0 + for i in range(N): + for j in range(M): + ans = max(ans, dfs(i, j)) + return ans \ No newline at end of file diff --git a/problems/python3/longest-palindromic-substring.py b/problems/python3/longest-palindromic-substring.py new file mode 100644 index 0000000..9edb477 --- /dev/null +++ b/problems/python3/longest-palindromic-substring.py @@ -0,0 +1,45 @@ +""" +Time: O(N^2) +Space: O(N^2) + +DP, TLE +""" +class Solution: + def longestPalindrome(self, s: str) -> str: + ans = s[0] + N = len(s) + dp = [[False]*N for _ in range(N)] + + for i in range(N): dp[i][i] = True + + for l in range(2, N+1): + for i in range(N): + j = i+l-1 + if j>=N: continue + dp[i][j] = s[i]==s[j] and (dp[i+1][j-1] or j-1 str: + N = len(s) + ans = s[0] + + for i in range(N): + l, r = i, i + while l>=0 and rlen(ans): ans = s[l:r+1] + l -= 1 + r += 1 + + l, r = i, i+1 + while l>=0 and rlen(ans): ans = s[l:r+1] + l -= 1 + r += 1 + return ans \ No newline at end of file diff --git a/problems/python3/maximum-product-subarray.py b/problems/python3/maximum-product-subarray.py new file mode 100644 index 0000000..243bf41 --- /dev/null +++ b/problems/python3/maximum-product-subarray.py @@ -0,0 +1,25 @@ +""" +Time: O(N) +Space: O(N), can be reduce to O(1) + +dp[i][0] := The max product from subarray that end with nums[i] +dp[i][1] := The min product from subarray that end with nums[i] +""" +class Solution: + def maxProduct(self, nums: List[int]) -> int: + N = len(nums) + dp = [[1, 1] for _ in range(N+1)] + ans = float('-inf') + + for i in range(1, N+1): + dp[i][0] = dp[i][1] = nums[i-1] + + if nums[i-1]>0: + dp[i][0] = max(dp[i][0], nums[i-1]*dp[i-1][0]) + dp[i][1] = min(dp[i][1], nums[i-1]*dp[i-1][1]) + else: + dp[i][0] = max(dp[i][0], nums[i-1]*dp[i-1][1]) + dp[i][1] = min(dp[i][1], nums[i-1]*dp[i-1][0]) + ans = max(ans, dp[i][0]) + + return ans \ No newline at end of file diff --git a/problems/python3/min-cost-climbing-stairs.py b/problems/python3/min-cost-climbing-stairs.py new file mode 100644 index 0000000..c59c446 --- /dev/null +++ b/problems/python3/min-cost-climbing-stairs.py @@ -0,0 +1,16 @@ +""" +Time: O(N) +Space: O(N), can further reduce to using only 2 variables -> O(1). + +dp[i] := the cost to get to index i. +""" +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + N = len(cost) + dp = [float('inf')]*(N+1) + dp[0] = 0 + dp[1] = 0 + + for i in range(2, len(dp)): + dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]) + return dp[-1] \ No newline at end of file diff --git a/problems/python3/min-cost-to-connect-all-points.py b/problems/python3/min-cost-to-connect-all-points.py new file mode 100644 index 0000000..4cd6ada --- /dev/null +++ b/problems/python3/min-cost-to-connect-all-points.py @@ -0,0 +1,28 @@ +class Solution: + def minCostConnectPoints(self, points: List[List[int]]) -> int: + ans = 0 + visited = set() + adj = collections.defaultdict(list) + N = len(points) + + #build adjacency list + for i in range(N): + x0, y0 = points[i] + for j in range(i+1, N): + x1, y1 = points[j] + dis = abs(x0-x1)+abs(y0-y1) + adj[(x0, y0)].append((dis, x1, y1)) + adj[(x1, y1)].append((dis, x0, y0)) + + h = [(0, points[0][0], points[0][1])] #min heap + while len(visited) int: + N = len(nums) + ans = 0 + + for n in range(N+1): + ans ^= n + + for n in nums: + ans ^= n + + return ans \ No newline at end of file diff --git a/problems/python3/multiply-strings.py b/problems/python3/multiply-strings.py new file mode 100644 index 0000000..be9f675 --- /dev/null +++ b/problems/python3/multiply-strings.py @@ -0,0 +1,22 @@ +class Solution: + def multiply(self, num1: str, num2: str) -> str: + if num1=='0' or num2=='0': return '0' + M, N = len(num1), len(num2) + temp = [0]*(M+N+1) + + num1, num2 = num1[::-1], num2[::-1] + for i in range(M): + for j in range(N): + digits = int(num1[i])*int(num2[j]) + temp[i+j] += digits + temp[i+j+1] += temp[i+j]//10 + temp[i+j] = temp[i+j]%10 + + ans = '' + temp = temp[::-1] + isLeadingZero = True + for d in temp: + if d!=0 or not isLeadingZero: + isLeadingZero = False + ans += str(d) + return ans \ No newline at end of file diff --git a/problems/python3/network-delay-time.py b/problems/python3/network-delay-time.py new file mode 100644 index 0000000..6da8613 --- /dev/null +++ b/problems/python3/network-delay-time.py @@ -0,0 +1,23 @@ +class Solution: + def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: + ans = 0 + adj = collections.defaultdict(list) + h = [] + visited = set() + + for u, v, w in times: + adj[u].append((v, w)) + + heapq.heappush(h, (0, k)) + while h: + timeNeededToGetHere, node = heapq.heappop(h) + + if node in visited: continue + visited.add(node) + ans = max(ans, timeNeededToGetHere) + + for nei, time in adj[node]: + if nei in visited: continue + heapq.heappush(h, (time+timeNeededToGetHere, nei)) + + return ans if len(visited)==n else -1 \ No newline at end of file diff --git a/problems/python3/number-of-1-bits.py b/problems/python3/number-of-1-bits.py new file mode 100644 index 0000000..2273859 --- /dev/null +++ b/problems/python3/number-of-1-bits.py @@ -0,0 +1,10 @@ +""" +n = n&(n-1) will turn the right most 1 to 0. +""" +class Solution: + def hammingWeight(self, n: int) -> int: + ans = 0 + while n>0: + n = n&(n-1) + ans += 1 + return ans \ No newline at end of file diff --git a/problems/python3/palindromic-substrings.py b/problems/python3/palindromic-substrings.py new file mode 100644 index 0000000..7ef7e96 --- /dev/null +++ b/problems/python3/palindromic-substrings.py @@ -0,0 +1,16 @@ +class Solution: + def countSubstrings(self, s: str) -> int: + def countPalindrome(l, r) -> int: + count = 0 + while l>=0 and r bool: + total = sum(nums) + if total%2!=0: return False + + target = total/2 + possibleSum = set() + possibleSum.add(0) + for num in nums: + temp = set() + for p in possibleSum: + if p==target or p+num==target: return True + temp.add(p) + temp.add(p+num) + possibleSum = temp + return False \ No newline at end of file diff --git a/problems/python3/plus-one.py b/problems/python3/plus-one.py new file mode 100644 index 0000000..8cfedb4 --- /dev/null +++ b/problems/python3/plus-one.py @@ -0,0 +1,16 @@ +class Solution: + def plusOne(self, digits: List[int]) -> List[int]: + i = len(digits)-1 + needAdditionDigit = True + + while i>=0 and needAdditionDigit: + if digits[i]==9: + digits[i] = 0 + i -= 1 + needAdditionDigit = True + else: + digits[i] += 1 + needAdditionDigit = False + if needAdditionDigit: digits.insert(0, 1) + return digits + \ No newline at end of file diff --git a/problems/python3/powx-n.py b/problems/python3/powx-n.py new file mode 100644 index 0000000..2b1b4fc --- /dev/null +++ b/problems/python3/powx-n.py @@ -0,0 +1,14 @@ +class Solution: + def myPow(self, x: float, k: int) -> float: + if k<0: return 1/self.myPow(x, -k) + + if k==0: + return 1 + elif k==1: + return x + elif k%2==0: + half = self.myPow(x, k//2) + return half * half + else: + half = self.myPow(x, (k-1)//2) + return half * half * x \ No newline at end of file diff --git a/problems/python3/reconstruct-itinerary.py b/problems/python3/reconstruct-itinerary.py new file mode 100644 index 0000000..14c928f --- /dev/null +++ b/problems/python3/reconstruct-itinerary.py @@ -0,0 +1,27 @@ +""" +DFS with backtracking. +""" +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + def dfs(start) -> bool: + if len(ans)==len(tickets)+1: return True + if start not in adj: return False + + temp = list(adj[start]) + for i, arr in enumerate(temp): + adj[start].pop(i) + ans.append(arr) + if dfs(arr): return True + adj[start].insert(i, arr) + ans.pop() + return False + + ans = ['JFK'] + adj = collections.defaultdict(list) + + tickets.sort() + for des, arr in tickets: + adj[des].append(arr) + + dfs('JFK') + return ans \ No newline at end of file diff --git a/problems/python3/regular-expression-matching.py b/problems/python3/regular-expression-matching.py new file mode 100644 index 0000000..93b44e6 --- /dev/null +++ b/problems/python3/regular-expression-matching.py @@ -0,0 +1,24 @@ +class Solution: + def isMatch(self, s: str, p: str) -> bool: + def dfs(i, j): + if (i, j) in cache: return cache[(i, j)] + if i>=M and j>=N: return True + if j>=N: return False + + match = i int: + res = 0 + for i in range(32): + bit = (n >> i) & 1 + res = res | (bit << (31 - i)) + return res \ No newline at end of file diff --git a/problems/python3/reverse-integer.py b/problems/python3/reverse-integer.py new file mode 100644 index 0000000..76137ab --- /dev/null +++ b/problems/python3/reverse-integer.py @@ -0,0 +1,16 @@ +class Solution: + def reverse(self, x: int) -> int: + MAX = 2**31-1 + MIN = -2**31 + ans = 0 + + while x: + digit = int(math.fmod(x, 10)) + x = int(x/10) + + if ans>MAX//10 or (ans==MAX//10 and digit>MAX%10): return 0 + if ans None: + l, r = 0, len(matrix[0])-1 + + while l None: + M = len(matrix) + N = len(matrix[0]) + firstRowZero = False + + for i in range(M): + for j in range(N): + if matrix[i][j]==0: + matrix[0][j] = 0 + if i==0: + firstRowZero = True + else: + matrix[i][0] = 0 + + for i in range(1, M): + if matrix[i][0]==0: + for j in range(N): + matrix[i][j] = 0 + + for j in range(N): + if matrix[0][j]==0: + for i in range(M): + matrix[i][j] = 0 + + if firstRowZero: + for j in range(N): + matrix[0][j] = 0 \ No newline at end of file diff --git a/problems/python3/single-number.py b/problems/python3/single-number.py new file mode 100644 index 0000000..daa3268 --- /dev/null +++ b/problems/python3/single-number.py @@ -0,0 +1,15 @@ +""" +^ (XOR) +The same will be 0 +0^0 = 0 +1^1 = 0 + +Different will be 1 +1^0 = 1 +0^1 = 1 +""" +class Solution: + def singleNumber(self, nums: List[int]) -> int: + ans = 0 + for num in nums: ans ^= num + return ans \ No newline at end of file diff --git a/problems/python3/spiral-matrix.py b/problems/python3/spiral-matrix.py new file mode 100644 index 0000000..ddce35c --- /dev/null +++ b/problems/python3/spiral-matrix.py @@ -0,0 +1,90 @@ +""" +Original's solution +Time: O(MN) +Space: O(1) +""" +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + ans = [] + x0 = 0 + y0 = 0 + dx = len(matrix[0])-1 + dy = len(matrix)-1 + direction = 'right' + isFirst = True + + ans.append(matrix[x0][y0]) + while True: + if direction=='right': + for x in range(x0+1, x0+dx+1): + ans.append(matrix[y0][x]) + x0 += dx + direction = 'down' + + if isFirst: + isFirst = False + else: + dx -= 1 + + if dy==0: break + + elif direction=='left': + for x in range(x0-1, x0-dx-1, -1): + ans.append(matrix[y0][x]) + x0 -= dx + direction = 'up' + dx -= 1 + if dy==0: break + + elif direction=='down': + for y in range(y0+1, y0+dy+1): + ans.append(matrix[y][x0]) + y0 += dy + direction = 'left' + dy -= 1 + if dx==0: break + + elif direction=='up': + for y in range(y0-1, y0-dy-1, -1): + ans.append(matrix[y][x0]) + y0 -= dy + direction = 'right' + dy -= 1 + if dx==0: break + return ans + + +""" +Answer from Neetcode, more elegant. +left, right, top, bottom is the border (index is exclusive on the border. +In other words, for matrix[i][j] +i: top List[int]: + res = [] + left, right = 0, len(matrix[0]) + top, bottom = 0, len(matrix) + + while left < right and top < bottom: + # get every i in the top row + for i in range(left, right): + res.append(matrix[top][i]) + top += 1 + # get every i in the right col + for i in range(top, bottom): + res.append(matrix[i][right - 1]) + right -= 1 + if not (left < right and top < bottom): + break + # get every i in the bottom row + for i in range(right - 1, left - 1, -1): + res.append(matrix[bottom - 1][i]) + bottom -= 1 + # get every i in the left col + for i in range(bottom - 1, top - 1, -1): + res.append(matrix[i][left]) + left += 1 + + return res \ No newline at end of file diff --git a/problems/python3/sum-of-two-integers.py b/problems/python3/sum-of-two-integers.py new file mode 100644 index 0000000..25f16f9 --- /dev/null +++ b/problems/python3/sum-of-two-integers.py @@ -0,0 +1,12 @@ +""" +Does not work with negative values yet. +""" +class Solution: + def getSum(self, a: int, b: int) -> int: + ans = a^b + carry = (a&b)<<1 + + while carry!=0: + ans, carry = ans^carry, (ans&carry)<<1 + + return ans \ No newline at end of file diff --git a/problems/python3/swim-in-rising-water.py b/problems/python3/swim-in-rising-water.py new file mode 100644 index 0000000..dd5dbef --- /dev/null +++ b/problems/python3/swim-in-rising-water.py @@ -0,0 +1,23 @@ +""" +Time: O(N^2 * LogN^2) = O(N^2 * 2LogN) = O(N^2LogN), N is the number of elements in a row or column. +Space: O(N^2) +""" +class Solution: + def swimInWater(self, grid: List[List[int]]) -> int: + ROWS = len(grid) + COLS = len(grid[0]) + + visited = set() + h = [(grid[0][0], 0, 0)] + + while h: + t, r0, c0 = heapq.heappop(h) + + if (r0, c0) in visited: continue + visited.add((r0, c0)) + if r0==ROWS-1 and c0==COLS-1: return t + + for r, c in ((r0+1, c0), (r0-1, c0), (r0, c0+1), (r0, c0-1)): + if r<0 or c<0 or r>=ROWS or c>=COLS: continue + if (r, c) in visited: continue + heapq.heappush(h, (max(t, grid[r][c]), r, c)) \ No newline at end of file diff --git a/problems/python3/target-sum.py b/problems/python3/target-sum.py new file mode 100644 index 0000000..832fadd --- /dev/null +++ b/problems/python3/target-sum.py @@ -0,0 +1,25 @@ +""" +Time: O(NS), S is sum(nums), N is len(nums). This is the max possible number of element in "history". Which will be lesser than 2^N. +Space: O(NS) +""" +class Solution: + def findTargetSumWays(self, nums: List[int], target: int) -> int: + def dfs(i, curr): + #cache + if (i, curr) in history: + return history[(i, curr)] + + #ending condition + if i==len(nums): + if curr==target: + history[(i, curr)] = 1 + else: + history[(i, curr)] = 0 + return history[(i, curr)] + + history[(i, curr)] = dfs(i+1, curr+nums[i])+dfs(i+1, curr-nums[i]) + return history[(i, curr)] + + ans = 0 + history = {} + return dfs(0, 0) \ No newline at end of file diff --git a/problems/python3/unique-paths.py b/problems/python3/unique-paths.py new file mode 100644 index 0000000..9c7ce40 --- /dev/null +++ b/problems/python3/unique-paths.py @@ -0,0 +1,11 @@ +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + m = m-1 #number of steps need to move down + n = n-1 #number of steps need to move right + + #the total combination of m and n to sort will be (m+n)! + #since all "move down" are consider the same, we need to remove the repeatition of it sorting: m!. + #since all "move right" are consider the same, we need to remove the repeatition of it sorting: n!. + #(m+n)!/m!n! + + return math.factorial(m+n)//(math.factorial(m)*math.factorial(n)) \ No newline at end of file diff --git a/problems/python3/word-break.py b/problems/python3/word-break.py new file mode 100644 index 0000000..8d2c817 --- /dev/null +++ b/problems/python3/word-break.py @@ -0,0 +1,23 @@ +""" +Time: O(N^2 * M). N is the length of the s. M is the number of word in wordDict. +Note that s[i:i+len(word)]==word takes O(N) time. + +Space: O(N) for the recursion memory stack size. + +dfs(i) := will return starting at index i, if i to the end the string can be separated. +""" +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + def dfs(i): + if i==len(s): return True + if i in history and not history[i]: return False + + for word in wordDict: + if i+len(word)<=len(s) and s[i:i+len(word)]==word: + history[i] = True + if dfs(i+len(word)): return True + history[i] = False + return False + + history = {} + return dfs(0) \ No newline at end of file diff --git a/problems/python3/word-ladder.py b/problems/python3/word-ladder.py new file mode 100644 index 0000000..453fb4a --- /dev/null +++ b/problems/python3/word-ladder.py @@ -0,0 +1,40 @@ +""" +Time: O(NxM^2). N is the number of words. M is the length of the word. +Note that, getPatterns() takes O(M^2) since creating new string will also takes O(M) and for each word we do that O(M) times. + +Space: O(NxM^2) +""" +class Solution: + def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: + def getPatterns(word) -> List[str]: + patterns = [] + for i in range(len(word)): + pattern = word[:i]+'*'+word[i+1:] + patterns.append(pattern) + return patterns + + + if endWord not in wordList: return 0 + wordList.append(beginWord) + + #build adjacency list + nei = collections.defaultdict(list) + for word in wordList: + for pattern in getPatterns(word): + nei[pattern].append(word) + + #BFS + q = collections.deque([(beginWord, 1)]) + visited = set() + while q: + word, steps = q.popleft() + + if word in visited: continue + visited.add(word) + if word==endWord: return steps + + for pattern in getPatterns(word): + for nextWord in nei[pattern]: + if nextWord in visited: continue + q.append((nextWord, steps+1)) + return 0 \ No newline at end of file