↓快轉到主要內容

Union-Find

Number Theory 數論
·4 分鐘
演算法 Math Number-Theory Prime Sieve Union-Find Gcd
GCD、判斷質數、埃氏篩 / 線性篩 / 最小質因數(SPF)、質因數分解後用 Union-Find 連起來,以及用質因數的最後出現位置切陣列
BFS 與圖的遍歷
·3 分鐘
演算法 Bfs Dfs Graph Matrix Union-Find
連通塊、網格 flood fill、多源 BFS、狀態空間 BFS、最短環,以及遞迴 DFS 的深度問題
LeetCode 827: Making A Large Island
·2 分鐘
LeetCode Hard Dfs Bfs Matrix Union-Find
LeetCode 827 解題筆記:先幫每座島編號並記大小,再對每個 0 看四周有哪些不同的島,大小相加再加 1
LeetCode 305: Number of Islands II
·2 分鐘
LeetCode Hard Union-Find Hash-Table Array
LeetCode 305 解題筆記:每加一格陸地島數加 1,跟四周已經是陸地的格子合併,每成功合併一次減 1;重複的位置要跳過
LeetCode 1998: GCD Sort of an Array
·2 分鐘
LeetCode Hard Math Number-Theory Union-Find Sieve Sorting Array
LeetCode 1998 解題筆記:同一個連通塊裡的數可以任意交換;用 SPF 把數字跟質因數合併,排序後每個位置的新舊數字在同一組就能排好
LeetCode 1627: Graph Connectivity With Threshold
·1 分鐘
LeetCode Hard Math Number-Theory Union-Find Array
LeetCode 1627 解題筆記:不用兩兩算 GCD;對每個 z > threshold,把 z 的所有倍數連起來,總共 O(n log n) 次合併
LeetCode 323: Number of Connected Components in an Undirected Graph
·2 分鐘
LeetCode Medium Graph Dfs Bfs Union-Find
LeetCode 323 解題筆記:DFS 或 Union-Find 數連通塊;Union-Find 沒有路徑壓縮會慢很多
LeetCode 3873: Maximum Points Activated with One Addition
·1 分鐘
LeetCode Hard Union-Find Hash-Table Array
LeetCode 3873 解題筆記:同 x 或同 y 的點用 Union-Find 合併成塊;新加的點最多接上兩塊,答案是最大的兩塊加 1
LeetCode 2421: Number of Good Paths
·2 分鐘
LeetCode Hard Tree Union-Find Graph Sorting
LeetCode 2421 解題筆記:依值由小到大加入節點,用 Union-Find 數同一群裡同值的節點
LeetCode 2382: Maximum Segment Sum After Removals
·1 分鐘
LeetCode Hard Union-Find Prefix-Sum Ordered-Set Array
LeetCode 2382 解題筆記:倒著做,把刪除變成加回去;Union-Find 的根記區段總和,最大值只會變大
LeetCode 3910: Count Connected Subgraphs with Even Node Sum
·1 分鐘
LeetCode Hard Bit-Manipulation Enumeration Graph Union-Find
LeetCode 3910 解題筆記:n 很小,用位元遮罩枚舉所有子集合,先檢查和是不是偶數,再檢查誘導子圖是否連通
LeetCode 1971: Find if Path Exists in Graph
·2 分鐘
LeetCode Easy Graph Union-Find
LeetCode 1971 解題筆記,依照原始 learning note 重新整理