https://leetcode.com/problems/the-maze Solution m := len(maze) n := len(maze[0]) Time Complexity: O(m * n) Space Complexity: O(m * n) (The input and output generally do not count towards the space com ...
https://leetcode.com/problems/kth-largest-element-in-an-array Solution n := len(nums) Time Complexity: O(n * log(k)) Space Complexity: O(k) (The input and output generally do not count towards the spa ...
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array Solution n := len(nums) Time Complexity: O(3 ** n)O(n) (due to memoization) Space Complexity: O(n) (The input and output ...
math Module math.inf ∞, ≡ float(“inf”) math.prod(iterable, *, start=1) def prod(iterable, *, start=1): for element in iterable: start *= element return start math.floor(f) -> in ...
https://leetcode.com/contest/weekly-contest-358/problems/apply-operations-to-maximize-score/ First Solution (Time Limit Exceeded) Time Complexity: O(n * (A + n)) A := max(nums) n := len(nums) compute ...
https://leetcode.com/contest/weekly-contest-357/problems/find-the-safest-path-in-a-grid/ Solution 計算 min_dis (queue/BFS) m := len(grid) n := len(grid[0]) min_dis[i][j] := min([manhattan_distance((i, j ...
@lru_cache[(maxsize=128, typed=False)] least recently used cache(memoize): 會依照 parameters 記錄 return 結果 再次以相同 parameters 呼叫此函式時 將略過計算,直接回傳之前的記錄結果 上限為最近使用的 maxsize 個 若 maxsize 為 None 則無上限 若 typed 為 Tru ...
In C do{ ... } while(condition) In Python while True: ... if not (condition): break
https://leetcode.com/problems/coin-change-ii Solution: Top-Down DP 用 (amount, len(coins)) 作為 self.memo 的key Time Complexity: O(n * amount) Space Complexity: O(n * amount) 1 <= coins[i] <= 5000 n ...
https://leetcode.com/problems/maximal-square Solution square := {top, bottom, left, right} side_length(square) := (bottom – top + 1) == (right – left + 1) dp[i][j] := side length of the ma ...