Medium
LeetCode 1750: Minimum Length of String After Deleting Similar Ends
·1 分鐘
LeetCode
Medium
Two-Pointers
String
LeetCode 1750 解題筆記:兩端字元相同時,各自把同一段相同字元全部吃掉;整段都是同一個字元時可以刪光
LeetCode 1616: Split Two Strings to Make Palindrome
·2 分鐘
LeetCode
Medium
Two-Pointers
String
Greedy
LeetCode 1616 解題筆記:外側用 a 的前綴配 b 的後綴貪心比對,配不下去時,剩下的中間段必須整段在 a 或 b 裡是回文;兩個方向都要檢查
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 分鐘
LeetCode
Medium
Two-Pointers
Sorting
Binary-Search
Array
LeetCode 1498 解題筆記:子序列只看最小值和最大值,排序後順序無關;固定最小值 nums[left],中間的元素任意選,共 2^(right - left) 種
LeetCode 1156: Swap For Longest Repeated Character Substring
·1 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
String
LeetCode 1156 解題筆記:對每個字元 c 找「最多一個不是 c」的最長視窗,換進來的 c 必須來自別處,所以長度不能超過 c 的總數
LeetCode 2401: Longest Nice Subarray
·1 分鐘
LeetCode
Medium
Sliding-Window
Bit-Manipulation
Array
LeetCode 2401 解題筆記:兩兩 AND 為 0 ⇔ 每個 bit 最多被一個數用到;用一個 mask 記錄視窗用掉的 bit,衝突就縮左邊並把它的 bit 拿掉
LeetCode 1712: Ways to Split Array Into Three Subarrays
·2 分鐘
LeetCode
Medium
Binary-Search
Prefix-Sum
Two-Pointers
LeetCode 1712 解題筆記:固定第一刀,第二刀的合法範圍用兩次 bisect 或兩個單調指針找出來
LeetCode 825: Friends Of Appropriate Ages
·2 分鐘
LeetCode
Medium
Two-Pointers
Sorting
Binary-Search
Array
LeetCode 825 解題筆記:排序後,每個人能送出邀請的對象是年齡落在 (0.5 × age + 7, age] 的一段區間,用兩個同向指針維護區間再扣掉自己
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 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 2962: Count Subarrays Where Max Element Appears at Least K Times
·1 分鐘
LeetCode
Medium
Sliding-Window
Array
LeetCode 2962 解題筆記:「至少 k 次」的計數視窗:最大值出現 k 次時一直縮左邊,縮完後左端在 [0, left) 的子陣列都合法,count += left