Two-Pointers
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 3775: Reverse Words With Same Vowel Count
·1 min
LeetCode
Medium
String
Simulation
Two-Pointers
LeetCode 3775 note: count the first word’s vowels, then reverse every later word with the same count
LeetCode 2697: Lexicographically Smallest Palindrome
·2 mins
LeetCode
Easy
Two-Pointers
String
Greedy
LeetCode 2697 note: converging pointers, setting both ends to the smaller character whenever they differ
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 1782: Count Pairs Of Nodes
·3 mins
LeetCode
Hard
Two-Pointers
Graph
Sorting
Hash-Table
Counting
LeetCode 1782 note: count pairs with deg[a] + deg[b] > q using sorted degrees and converging pointers, then correct the over-count for pairs that share edges
LeetCode 1750: Minimum Length of String After Deleting Similar Ends
·3 mins
LeetCode
Medium
Two-Pointers
String
LeetCode 1750 note: when both ends match, each side eats its whole run of that character; a string of one repeated character can be deleted entirely
LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode
Medium
Two-Pointers
String
Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking