↓快轉到主要內容

Math

LeetCode 4098: Minimize the Maximum Value of GCD-Connected Array
·2 分鐘
LeetCode Medium Math Number-Theory Greedy
LeetCode 4098 解題筆記:value 的最小質因數 p 決定一切:後一個最小是 value + p,之後都是偶數每次加 2;左邊可行的條件是 value - p >= 2 * index
Number Theory 數論
·4 分鐘
演算法 Math Number-Theory Prime Sieve Union-Find Gcd
GCD、判斷質數、埃氏篩 / 線性篩 / 最小質因數(SPF)、質因數分解後用 Union-Find 連起來,以及用質因數的最後出現位置切陣列
Math 數學技巧
·5 分鐘
演算法 Math Combinatorics Geometry Brainteaser
組合計數(配對數、切分方式)、第 k 個排列逐位決定、幾何(最近點、餘弦定理、三角不等式)、排序或觀察之後變成公式的題目,以及直式進位、進位制轉換和快速冪
LeetCode 775: Global and Local Inversions
·2 分鐘
LeetCode Medium Array Math
LeetCode 775 解題筆記:把計數問題轉成存在性問題,O(n) 解決
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 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 996: Number of Squareful Arrays
·2 分鐘
LeetCode Hard Dynamic-Programming Bitmask Backtracking Math Hash-Table
LeetCode 996 解題筆記:n 最多 12,用 bitmask DP:dp[mask][last] = 用了 mask 這些數、最後一個是 last 的排列數;相同的數規定依 index 順序使用來去重
LeetCode 1354: Construct Target Array With Multiple Sums
·1 分鐘
LeetCode Hard Heap Math Greedy
LeetCode 1354 解題筆記:反著做,最大值一定是最後被設定的;差距大時用取餘數一次跳完
LeetCode 2963: Count the Number of Good Partitions
·1 分鐘
LeetCode Hard Math Combinatorics Hash-Table Array
LeetCode 2963 解題筆記:同一個數的所有出現位置必須在同一段,用最後出現位置把陣列切成 b 個不可再分的區塊;區塊之間可切可不切,答案是 2^(b-1)
LeetCode 2376: Count Special Integers
·1 分鐘
LeetCode Hard Dynamic-Programming Digit-Dp Math Bitmask
LeetCode 2376 解題筆記:數位 DP,狀態 (pos, tight, started, mask);mask 記錄用過的數字,前導零不能算成用過 0