https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Solution 使用BFS,使上層的 node 得以被優先處理 如果 cur.next 存在 ⇒ 說明 cur 同一層中還有右節點 ⇒ cur.right.next = cur.next.left class Solution: def conne ...
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Solution Key Point 先買在低谷 (valley),再賣在高峰 (peak) class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 valley = p ...
https://leetcode.com/problems/best-sightseeing-pair/submissions/ Solution Key Point score(i, j) := values[i] + values[j] + i – j, i < j 不管 j 為多少,「 values[i] + i 」部分是不改變的 class Solution ...
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Solution Python 版 class Solution: def maxProfit(self, prices: List[int]) -> int: min_price = float("inf") max_profit = 0 for price in ...
構造為 tree 每一個親節點對任何一個子節點都保持著某一特定關係 例:parent </≤/>/≥ children 所有的子樹的親子節點也都是相同特定關係的 heap ※ 通常說 heap 指的即是 binary heap Binary Heap 符合所有 heap 的條件 為一 complete binary tree [Python] 使用 heapq 實作 b ...
定義 binary tree 的每一個 node 最多只有兩個 subnode (子節點) Complete Binary Tree(完全二元樹) 最後一層以外的層全部都是滿的 最後一層的節點全部向左靠 Binary Search Tree \begin{align} & \forall \text{ internal node } n \text{:} \\ & \text{1. ...
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