Array
LeetCode 2563: Count the Number of Fair Pairs
·2 分鐘
LeetCode
Medium
Two-Pointers
Sorting
Binary-Search
Array
LeetCode 2563 解題筆記:把「和落在 [lower, upper]」拆成「和 <= upper」減「和 <= lower - 1」,每個都用排序後的對撞指針 O(n) 數
LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted
·1 分鐘
LeetCode
Medium
Two-Pointers
Binary-Search
Array
Monotonic-Stack
LeetCode 1574 解題筆記:留下來的一定是「非遞減前綴 + 非遞減後綴」;先找出最長前綴和後綴,再用雙指針把兩段接起來
LeetCode 1471: The k Strongest Values in an Array
·1 分鐘
LeetCode
Medium
Two-Pointers
Sorting
Array
LeetCode 1471 解題筆記:排序後離中位數最遠的一定在兩端,對撞指針每次從兩端挑比較強的那個,挑 k 次
LeetCode 948: Bag of Tokens
·1 分鐘
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
LeetCode 948 解題筆記:排序後,用最小的 token 換分數、用最大的 token 換能量;只有分數 > 0 才能換能量,換不了就停
LeetCode 923: 3Sum With Multiplicity
·3 分鐘
LeetCode
Medium
Two-Pointers
Sorting
Hash-Table
Counting
Array
LeetCode 923 解題筆記:排序後固定第一個數、對撞指針找另外兩個;兩端值相同時用組合數一次算完,不同時把兩端各自的重複數相乘
LeetCode 2105: Watering Plants II
·1 分鐘
LeetCode
Medium
Two-Pointers
Simulation
Array
LeetCode 2105 解題筆記:Alice 從左、Bob 從右,對撞指針模擬澆水;相遇在同一株時由水比較多的人澆
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 分鐘
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
LeetCode 1877 解題筆記:排序後最小配最大、次小配次大,對撞指針取配對和的最大值
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 2401: Longest Nice Subarray
·1 分鐘
LeetCode
Medium
Sliding-Window
Bit-Manipulation
Array
LeetCode 2401 解題筆記:兩兩 AND 為 0 ⇔ 每個 bit 最多被一個數用到;用一個 mask 記錄視窗用掉的 bit,衝突就縮左邊並把它的 bit 拿掉
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 930: Binary Subarrays With Sum
·1 分鐘
LeetCode
Medium
Sliding-Window
Prefix-Sum
Hash-Table
Array
LeetCode 930 解題筆記:「和剛好是 goal」= 「和最多 goal」−「和最多 goal - 1」;或用前綴和 + 雜湊表