Array
LeetCode 2859: Sum of Values at Indices With K Set Bits
·1 分鐘
LeetCode
Easy
Array
Bit-Manipulation
LeetCode 2859 解題筆記:看的是 index 的二進位有幾個 1,不是值
LeetCode 154: Find Minimum in Rotated Sorted Array II
·1 分鐘
LeetCode
Hard
Array
Binary-Search
LeetCode 154 解題筆記:跟 LC 153 一樣跟右端比,相等時分不出斷點在哪一邊,只能 r -= 1;最壞 O(n)
LeetCode 611: Valid Triangle Number
·1 分鐘
LeetCode
Medium
Array
Two-Pointers
Binary-Search
Greedy
LeetCode 611 解題筆記:排序後固定最長邊 k,左右指針找兩短邊:和夠大時中間的都可以,一次加 j - i 個
LeetCode 540: Single Element in a Sorted Array
·1 分鐘
LeetCode
Medium
Array
Binary-Search
LeetCode 540 解題筆記:單獨的那個出現之前,每一對都從偶數 index 開始;二分看偶數位置 mid 和 mid + 1 是否相同
LeetCode 275: H-Index II
·1 分鐘
LeetCode
Medium
Array
Binary-Search
LeetCode 275 解題筆記:引用數已排序,找第一個 citations[i] >= n - i 的位置,答案是 n - i;用二分做到 O(log n)
LeetCode 1991: Find the Middle Index in Array
·1 分鐘
LeetCode
Easy
Array
Prefix-Sum
LeetCode 1991 解題筆記:先算總和,掃描時維護左邊的和,右邊的和是 total - left - nums[i]
LeetCode 1732: Find the Highest Altitude
·1 分鐘
LeetCode
Easy
Array
Prefix-Sum
LeetCode 1732 解題筆記:海拔就是 gain 的前綴和,起點是 0,取最大值
LeetCode 1480: Running Sum of 1d Array
·1 分鐘
LeetCode
Easy
Array
Prefix-Sum
LeetCode 1480 解題筆記:前綴和的定義,每一格加上前一格
LeetCode 1413: Minimum Value to Get Positive Step by Step Sum
·1 分鐘
LeetCode
Easy
Array
Prefix-Sum
LeetCode 1413 解題筆記:起始值加上每個前綴和都要 >= 1,所以起始值是 max(1, 1 - 最小前綴和)
LeetCode 2335: Minimum Amount of Time to Fill Cups
·1 分鐘
LeetCode
Easy
Array
Greedy
Sorting
Heap-Priority-Queue
LeetCode 2335 解題筆記:答案是 max(最多的那種, ceil(總數 / 2));模擬的話每秒從最多的兩種各裝一杯
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 分鐘
LeetCode
Easy
Array
Hash-Table
Sorting
Heap-Priority-Queue
LeetCode 2099 解題筆記:選出最大的 k 個數(連同 index),再按 index 排回原本的順序
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 分鐘
LeetCode
Easy
Array
Hash-Table
Greedy
Sorting
LeetCode 2357 解題筆記:每次操作會讓最小的正數那一群變成 0,所以次數就是不同的正數有幾種