LeetCode
LeetCode 3684: Maximize Sum of At Most K Distinct Elements
·1 分鐘
LeetCode
Easy
Greedy
Sorting
Hash-Table
LeetCode 3684 解題筆記:全是正數,去重後由大到小取前 k 個
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·1 分鐘
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
Monotonic-Stack
LeetCode 3555 解題筆記:每個長度 k 的視窗都是一次 LeetCode 581;n 只有 1000,對每個視窗跑 O(k) 的兩次掃描即可
LeetCode 2273: Find Resultant Array After Removing Anagrams
·1 分鐘
LeetCode
Easy
Hash-Table
String
Sorting
LeetCode 2273 解題筆記:一直刪掉跟前一個是異位詞的字,等於只保留跟前一個留下來的字不是異位詞的字;用排序後的字串比較
LeetCode 1793: Maximum Score of a Good Subarray
·1 分鐘
LeetCode
Hard
Two-Pointers
Greedy
Binary-Search
Array
Monotonic-Stack
LeetCode 1793 解題筆記:從 k 往兩邊擴張,每次往鄰居比較大的那一邊走,讓每個長度的最小值盡量大;到邊界時只能往另一邊
LeetCode 3323: Minimize Connected Groups by Inserting Interval
·1 分鐘
LeetCode
Medium
Sliding-Window
Sorting
Binary-Search
Array
LeetCode 3323 解題筆記:先合併區間;新區間能把第 l 到 r 組連起來 ⇔ 第 r 組的起點 − 第 l 組的終點 <= k;滑動視窗找一次能連起來最多組
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·1 分鐘
LeetCode
Medium
Two-Pointers
Greedy
Array
LeetCode 1989 解題筆記:鬼從左到右,每個鬼抓範圍內最左邊、還沒被抓的人;用一個只往右走的指針指向下一個候選人
LeetCode 2972: Count the Number of Incremovable Subarrays II
·1 分鐘
LeetCode
Hard
Two-Pointers
Binary-Search
Array
LeetCode 2972 解題筆記:刪掉一段後留下嚴格遞增的前綴 + 後綴;對每個合法的前綴結尾,用只往右走的指針找第一個能接上的後綴起點,後面的起點都能選
LeetCode 2122: Recover the Original Array
·2 分鐘
LeetCode
Hard
Two-Pointers
Sorting
Hash-Table
Enumeration
Array
LeetCode 2122 解題筆記:最小的數一定在 lower,枚舉它的配對決定 k;排序後用兩個指針把每個最小的未配對數配給 +2k 的數
LeetCode 1984: Minimum Difference Between Highest and Lowest of K Scores
·1 分鐘
LeetCode
Easy
Sliding-Window
Sorting
Array
LeetCode 1984 解題筆記:排序後選出的 k 個一定是連續的一段;固定長度 k 的視窗,差是兩端相減
LeetCode 658: Find K Closest Elements
·2 分鐘
LeetCode
Medium
Two-Pointers
Binary-Search
Sliding-Window
LeetCode 658 解題筆記:雙指針從兩端縮成長度 k,或二分搜尋窗口的起點
LeetCode 633: Sum of Square Numbers
·2 分鐘
LeetCode
Medium
Two-Pointers
Math
Binary-Search
LeetCode 633 解題筆記:a 從 0、b 從 isqrt(c) 開始對撞;平方和太大 b 減一,太小 a 加一;注意 0 也算、a 可以等於 b、浮點數開根號的誤差
LeetCode 3820: Pythagorean Distance Nodes in a Tree
·1 分鐘
LeetCode
Medium
Tree
Bfs
Graph
LeetCode 3820 解題筆記:三次 BFS 算出每個點到三個目標的距離