Hash-Table
LeetCode 1782: Count Pairs Of Nodes
·2 分鐘
LeetCode
Hard
Two-Pointers
Graph
Sorting
Hash-Table
Counting
LeetCode 1782 解題筆記:先用排序後的度數 + 對撞指針數 deg[a] + deg[b] > q 的配對,再對有共用邊的配對修正多算的部分
LeetCode 1156: Swap For Longest Repeated Character Substring
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
String
LeetCode 1156 解題筆記:對每個字元 c 找「最多一個不是 c」的最長視窗,換進來的 c 必須來自別處,所以長度不能超過 c 的總數
LeetCode 992: Subarrays with K Different Integers
·1 分鐘
LeetCode
Hard
Sliding-Window
Hash-Table
Counting
Array
LeetCode 992 解題筆記:「剛好 k 種」=「最多 k 種」−「最多 k - 1 種」,「最多」型的計數視窗每個右端加 right - left + 1
LeetCode 3306: Count of Substrings Containing Every Vowel and K Consonants II
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
String
LeetCode 3306 解題筆記:「剛好 k 個子音」不單調;改成「五個母音都有且子音至少 k 個」減「至少 k + 1 個」,兩個都是「至少」型的計數視窗
LeetCode 930: Binary Subarrays With Sum
·1 分鐘
LeetCode
Medium
Sliding-Window
Prefix-Sum
Hash-Table
Array
LeetCode 930 解題筆記:「和剛好是 goal」= 「和最多 goal」−「和最多 goal - 1」;或用前綴和 + 雜湊表
LeetCode 3298: Count Substrings That Can Be Rearranged to Contain a String II
·1 分鐘
LeetCode
Hard
Sliding-Window
Hash-Table
String
LeetCode 3298 解題筆記:能重排成以 word2 開頭 ⇔ 每個字元的數量都不少於 word2;「至少」型的計數視窗,用 meet 記錄有幾種字元已經達標
LeetCode 2537: Count the Number of Good Subarrays
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Array
LeetCode 2537 解題筆記:加入 x 會多出 cnt[x] 對相同的 pair,移出時少 cnt[x] - 1 對;「至少 k 對」的計數視窗,count += left
LeetCode 3325: Count Substrings With K-Frequency Characters I
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
String
LeetCode 3325 解題筆記:「至少一個字元出現 k 次」的計數視窗;剛加入的字元湊滿 k 次就一直縮,縮到它變成 k - 1 次為止
LeetCode 1358: Number of Substrings Containing All Three Characters
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
String
LeetCode 1358 解題筆記:「至少各一個 a、b、c」的計數視窗:三種都有時一直縮左邊,count += left
LeetCode 2875: Minimum Size Subarray in Infinite Array
·1 分鐘
LeetCode
Medium
Sliding-Window
Prefix-Sum
Hash-Table
Array
LeetCode 2875 解題筆記:先拿掉 target // sum 個完整週期,剩下的餘數比一整圈小,一定能在兩份 nums 接起來的陣列裡用滑動視窗找到
LeetCode 395: Longest Substring with At Least K Repeating Characters
·2 分鐘
LeetCode
Medium
Sliding-Window
Divide-and-Conquer
Hash-Table
String
LeetCode 395 解題筆記:「每個字元至少 k 次」不單調,不能直接滑動;分治法用出現不到 k 次的字元切開,或枚舉視窗裡有幾種字元,固定種類數後就能滑動
LeetCode 3810: Minimum Operations to Reach Target Array
·1 分鐘
LeetCode
Medium
Greedy
Hash-Table
Array
LeetCode 3810 解題筆記:一次操作會把某個值的所有位置都改對,所以答案是不相等的位置上有幾種不同的值