https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/ Solution 把 nums 以 0 分割為 part[0], part[1], … 如果 len(part[i]) % 2 為 0 (負數有偶數個,則其積為正) part[i] 的 Maximum Length of Sub ...
https://leetcode.com/problems/maximum-product-subarray/ Solution 參考 Leetcode # 53. Maximum Subarray (Kadane’s Algorithm) ※ 注意: 若先計算 max_ending_here 才計算 min_ending_here 會導致結果錯誤 〇 max_ending_here, min_e ...
https://leetcode.com/problems/maximum-sum-circular-subarray/ Solution
定義 當 pop() 時,會遵守: queue的特性:先進先出(first in, first out) 優先權 大/小 的優先 pop (max/min-priority queue) 用 Binary Heap 實作 Priority Queue [Python] 使用 heapq
https://leetcode.com/problems/jump-game-ii/ Solution (Dynamic Programming) class Solution: def jump(self, nums: List[int]) -> int: DP = [0] + [float("inf")]* (len(nums) - 1) for i in range(len(nums ...
分治法 Divide and Conquer 總是能夠將「問題 X 」拆分為「問題 A 」和「問題 B 」 滿足「問題 A 」+「問題 B 」=「問題 X 」 且能確定「問題 X 」的解只存在於「問題 A 」或「問題 B 」兩者其中之一 如果「問題 X 」的 brute force 解法之 time complexity 為 O(n) 則使用 binary method 的時間複雜度將減少為 O( ...
https://leetcode.com/problems/course-schedule-ii/ Explanation 使用 in-degree 關鍵在於 優先提取 in-degree 為 0 的 vertex(即該 vertex 已無任何前導 vertex) vertex被提取時,調整其子 vertex 的 in-degree 當 graph 中有 loop 當 graph 中有 loop ...
list 常用操作 Operation Syntax ATC Append an item x to the last lst.append(x) O(1) Remove and return the last (most left) item in the list lst.pop() O(1) Remove and return the ith item in the list lst.pop ...
https://leetcode.com/problems/longest-palindromic-substring/ Solution Dynamic Programming \begin{align*} & \text{定義 } P(i, j) \text{ 為:} \\ & P(i, j)= \begin{cases} True & \text{, if the s ...
https://leetcode.com/problems/numbers-at-most-n-given-digit-set/ Solution class Solution: def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int: n_str = str(n) l = len(n_str) dp = [0] * ...