Hash-Table
LeetCode 1016: Binary String With Substrings Representing 1 To N
·2 分鐘
LeetCode
Medium
Sliding-Window
String
Bit-Manipulation
Hash-Table
LeetCode 1016 解題筆記:s 最多 1000 個字元,長度固定時的子字串數量有限,n 太大一定是 False;從 n 往下檢查會很快失敗,或從每個起點往右最多延伸 30 位收集所有 <= n 的值
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Array
LeetCode 3795 解題筆記:求最短的可變視窗;同一個值只算一次,記錄每個值最後出現的位置,左邊界移出的是最後一次出現才扣掉
LeetCode 2653: Sliding Subarray Beauty
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Array
LeetCode 2653 解題筆記:值域只有 -50 到 50,固定視窗用計數陣列維護,每個視窗從 -50 往上累加找第 x 小的負數
LeetCode 3694: Distinct Points Reachable After Substring Removal
·1 分鐘
LeetCode
Medium
Sliding-Window
Prefix-Sum
Hash-Table
String
LeetCode 3694 解題筆記:終點 = 全部的位移 − 被刪掉那段的位移;用長度 k 的視窗維護那段的位移,把每個終點放進 set
LeetCode 3679: Minimum Discards to Balance Inventory
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Simulation
Counting
Array
LeetCode 3679 解題筆記:規則已經決定哪些要丟,直接模擬;長度 w 的視窗只計算「留下來」的到貨,被丟掉的離開視窗時不能再扣
LeetCode 2461: Maximum Sum of Distinct Subarrays With Length K
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Array
LeetCode 2461 解題筆記:固定長度 k 的視窗,不同值的個數剛好等於 k 才代表全部不重複
LeetCode 2841: Maximum Sum of Almost Unique Subarray
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Array
LeetCode 2841 解題筆記:固定長度 k 的視窗,維護總和與計數雜湊表;不同值的個數就是雜湊表的大小,數量歸零要刪 key
LeetCode 1512: Number of Good Pairs
·1 分鐘
LeetCode
Easy
Math
Hash-Table
Counting
Array
LeetCode 1512 解題筆記:邊掃邊數,每個數可以跟前面每個相同的數配一對,答案加上目前的出現次數
LeetCode 3731: Find Missing Elements
·1 分鐘
LeetCode
Easy
Hash-Table
Sorting
Array
LeetCode 3731 解題筆記:最小和最大值一定還在,枚舉中間的每個整數,用集合檢查有沒有出現
LeetCode 3712: Sum of Elements With Frequency Divisible by K
·1 分鐘
LeetCode
Easy
Hash-Table
Counting
Array
LeetCode 3712 解題筆記:先數每個值的次數,次數能被 k 整除的就加上「值 × 次數」
LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·1 分鐘
LeetCode
Medium
Greedy
Hash-Table
String
Counting
LeetCode 2131 解題筆記:每個字跟它的反轉配成一對放兩邊;本身是回文的字也能兩兩配對,只有剩下的一個能放正中間
LeetCode 1248: Count Number of Nice Subarrays
·2 分鐘
LeetCode
Medium
Sliding-Window
Prefix-Sum
Hash-Table
Array
LeetCode 1248 解題筆記:「剛好 k 個奇數」不單調,改成「最多 k 個」減「最多 k - 1 個」;或用前綴和 + 雜湊表