Hard
LeetCode LCR 170: 交易逆序對的總數
·2 分鐘
LeetCode
Hard
Merge-Sort
Divide-and-Conquer
Counting
LCR 170(劍指 Offer 51)解題筆記:merge 時順便數逆序對
LeetCode 315: Count of Smaller Numbers After Self
·2 分鐘
LeetCode
Hard
Merge-Sort
Divide-and-Conquer
Binary-Indexed-Tree
Counting
LeetCode 315 解題筆記:帶 index 的 merge sort,以及 BIT 解法
LeetCode 827: Making A Large Island
·2 分鐘
LeetCode
Hard
Dfs
Bfs
Matrix
Union-Find
LeetCode 827 解題筆記:先幫每座島編號並記大小,再對每個 0 看四周有哪些不同的島,大小相加再加 1
LeetCode 212: Word Search II
·1 分鐘
LeetCode
Hard
Trie
Backtracking
Matrix
LeetCode 212 解題筆記:把所有單字建成 Trie,網格 DFS 時同步走 Trie,找到就移除、空節點就剪掉
LeetCode 3203: Find Minimum Diameter After Merging Two Trees
·2 分鐘
LeetCode
Hard
Tree
Bfs
Dfs
Graph
Tree-Diameter
LeetCode 3203 解題筆記:連接兩棵樹的中心,答案是 max(d1, d2, ceil(d1/2) + ceil(d2/2) + 1)
LeetCode 305: Number of Islands II
·2 分鐘
LeetCode
Hard
Union-Find
Hash-Table
Array
LeetCode 305 解題筆記:每加一格陸地島數加 1,跟四周已經是陸地的格子合併,每成功合併一次減 1;重複的位置要跳過
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 1998: GCD Sort of an Array
·2 分鐘
LeetCode
Hard
Math
Number-Theory
Union-Find
Sieve
Sorting
Array
LeetCode 1998 解題筆記:同一個連通塊裡的數可以任意交換;用 SPF 把數字跟質因數合併,排序後每個位置的新舊數字在同一組就能排好