LeetCode
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 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 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 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