↓快轉到主要內容

LeetCode

LeetCode 253: Meeting Rooms II
·1 分鐘
LeetCode Medium Heap Sorting Intervals Sweep-Line
LeetCode 253 解題筆記:heap 存進行中會議的結束時間,半開區間要用 <=
LeetCode 1220: Count Vowels Permutation
·1 分鐘
LeetCode Hard Dynamic-Programming State-Machine
LeetCode 1220 解題筆記:狀態機 DP,狀態是最後一個母音;把「誰後面可以接誰」反過來寫成「誰可以接在誰後面」
LeetCode 4081: Prime Subset Selection II
·2 分鐘
LeetCode Hard Knapsack-Dp Number-Theory
LeetCode 4081 解題筆記:篩出質數後做 0/1 背包,dp[j] 是湊出 j 的最少個數;質數由大到小放,個數一樣時新的組合開頭比較小,所以用 <= 更新就是字典序最小
LeetCode 2751: Robot Collisions
·1 分鐘
LeetCode Hard Stack Simulation Sorting
LeetCode 2751 解題筆記:依位置排序,往右的機器人放進 stack,往左的遇到就一路碰撞到其中一方消失
LeetCode 2584: Split the Array to Make Coprime Products
·2 分鐘
LeetCode Hard Math Number-Theory Sieve Hash-Table Array
LeetCode 2584 解題筆記:兩段乘積互質 ⇔ 沒有質因數同時出現在兩段;記下每個質因數最後出現的 index,從左往右擴張 reach,reach == i 時就能在 i 後面切
LeetCode 2267: Check if There Is a Valid Parentheses String Path
·2 分鐘
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Bracket-Sequences
LeetCode 2267 解題筆記:狀態是 (r, c, 左括號餘額);餘額不能是負的,也不能超過剩下的步數;BFS / DP 搜尋這些狀態,走到終點時餘額為 0 就成立
LeetCode 2761: Prime Pairs With Target Sum
·1 分鐘
LeetCode Medium Math Number-Theory Sieve Enumeration Array
LeetCode 2761 解題筆記:篩出 < n 的質數,x 從小到大、只看到 n / 2,x 和 n - x 都是質數就是一對
LeetCode 2523: Closest Prime Numbers in Range
·1 分鐘
LeetCode Medium Math Number-Theory Sieve Array
LeetCode 2523 解題筆記:篩出範圍內的質數,相鄰的兩個質數才可能是最接近的;也可以從 left 往右試除,找到差是 2 的雙生質數就停
LeetCode 1998: GCD Sort of an Array
·2 分鐘
LeetCode Hard Math Number-Theory Union-Find Sieve Sorting Array
LeetCode 1998 解題筆記:同一個連通塊裡的數可以任意交換;用 SPF 把數字跟質因數合併,排序後每個位置的新舊數字在同一組就能排好
LeetCode 1001: Grid Illumination
·2 分鐘
LeetCode Hard Hash-Table Array
LeetCode 1001 解題筆記:列、欄、兩條對角線各開一個計數器(r - c、r + c),加上亮著的燈的集合;關燈時四個計數器都要減
LeetCode 4067: Longest Subarray With Restricted Pair Sums
·2 分鐘
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 4067 解題筆記:合法視窗的子陣列也合法,可以滑動;加入 x 時只要檢查跟 x 有關的三元組:x 當加數(x + y 在視窗裡)或 x 當和(x - y 在視窗裡)
LeetCode 1842: Next Palindrome Using Same Digits
·2 分鐘
LeetCode Hard Two-Pointers String
LeetCode 1842 解題筆記:回文由左半邊決定,中間的數字固定不動;對左半邊做 next permutation,再鏡像回去