Array
LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode
Medium
Two-Pointers
Sorting
Binary-Search
Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted
·2 mins
LeetCode
Medium
Two-Pointers
Binary-Search
Array
Monotonic-Stack
LeetCode 1574 note: what remains is a non-decreasing prefix plus a non-decreasing suffix; find the longest of each, then join them with two pointers
LeetCode 1471: The k Strongest Values in an Array
·2 mins
LeetCode
Medium
Two-Pointers
Sorting
Array
LeetCode 1471 note: after sorting, the value farthest from the median is always at one of the ends, so converging pointers pick the stronger end k times
LeetCode 948: Bag of Tokens
·2 mins
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode
Medium
Two-Pointers
Sorting
Hash-Table
Counting
Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 2105: Watering Plants II
·2 mins
LeetCode
Medium
Two-Pointers
Simulation
Array
LeetCode 2105 note: Alice from the left and Bob from the right, simulated with converging pointers; when they meet at the same plant, whoever has more water waters it
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode
Medium
Two-Pointers
Sorting
Binary-Search
Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 2401: Longest Nice Subarray
·2 mins
LeetCode
Medium
Sliding-Window
Bit-Manipulation
Array
LeetCode 2401 note: pairwise AND 0 ⇔ each bit is used by at most one number; a mask holds the window’s bits, and on a conflict the left shrinks and its bits are removed
LeetCode 992: Subarrays with K Different Integers
·2 mins
LeetCode
Hard
Sliding-Window
Hash-Table
Counting
Array
LeetCode 992 note: “exactly k kinds” = “at most k” − “at most k - 1”, where each at-most window adds right - left + 1 per right end
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode
Medium
Two-Pointers
Sorting
Binary-Search
Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself
LeetCode 930: Binary Subarrays With Sum
·2 mins
LeetCode
Medium
Sliding-Window
Prefix-Sum
Hash-Table
Array
LeetCode 930 note: “sum exactly goal” = “sum at most goal” − “sum at most goal - 1”; or prefix sums with a hash map