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 ...
a > b and b > c→a > b > c
https://leetcode.com/problems/search-in-rotated-sorted-array-ii Solution Leetcode # 33. Search in Rotated Sorted Array 的 solution 配合題目修改了 input 跟 output class Solution: def search(self, nums: List[int ...
https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs Solution: Greedy Method + Binary Search Greedy Method – count_valid_pairs(threshold) count_valid_pairs(threshold): 事先已將num ...