↓快轉到主要內容

Binary-Search

LeetCode 778: Swim in Rising Water
·2 分鐘
LeetCode Hard Graph Dijkstra Binary-Search
LeetCode 778 解題筆記,依照原始 learning note 重新整理
LeetCode 2838: Maximum Coins Heroes Can Collect
·1 分鐘
LeetCode Medium Binary-Search Prefix-Sum Sorting
LeetCode 2838 解題筆記:怪物依強度排序建金幣前綴和,每個英雄用 bisect_right 找能打贏的範圍
LeetCode 2054: Two Best Non-Overlapping Events
·1 分鐘
LeetCode Medium Dynamic-Programming Binary-Search Sorting Heap
LeetCode 2054 解題筆記:只能選兩個,所以對每個活動,加上「在它開始前就結束的活動裡價值最大的」;依結束時間排序,前綴最大值 + 二分搜尋
LeetCode 3824: Minimum K to Reduce Array Within Limit
·1 分鐘
LeetCode Medium Binary-Search Math
LeetCode 3824 解題筆記:對 k 二分,條件是單調的;上界至少要到 n,因為操作次數至少是 n
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 2972: Count the Number of Incremovable Subarrays II
·1 分鐘
LeetCode Hard Two-Pointers Binary-Search Array
LeetCode 2972 解題筆記:刪掉一段後留下嚴格遞增的前綴 + 後綴;對每個合法的前綴結尾,用只往右走的指針找第一個能接上的後綴起點,後面的起點都能選
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 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 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) 種