↓Skip to main content

LeetCode

LeetCode 3684: Maximize Sum of At Most K Distinct Elements
·1 min
LeetCode Easy Greedy Sorting Hash-Table
LeetCode 3684 note: all values are positive, so deduplicate and take the k largest in descending order
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 2273: Find Resultant Array After Removing Anagrams
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 2273 note: repeatedly deleting a word that is an anagram of the previous one keeps exactly the words that are not anagrams of the last kept word; compare sorted strings
LeetCode 1793: Maximum Score of a Good Subarray
·3 mins
LeetCode Hard Two-Pointers Greedy Binary-Search Array Monotonic-Stack
LeetCode 1793 note: expand from k toward the larger neighbor each step so the minimum stays as large as possible for each length; at a boundary, only the other side can grow
LeetCode 3323: Minimize Connected Groups by Inserting Interval
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3323 note: merge first; a new interval can join groups l through r ⇔ start of group r − end of group l <= k, so a sliding window finds the most groups one interval can connect
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·3 mins
LeetCode Medium Two-Pointers Greedy Array
LeetCode 1989 note: process the “it” players left to right, each catching the leftmost uncaught person in range, with a right-only pointer to the next candidate
LeetCode 2972: Count the Number of Incremovable Subarrays II
·2 mins
LeetCode Hard Two-Pointers Binary-Search Array
LeetCode 2972 note: what remains is a strictly increasing prefix plus suffix; for each valid prefix end, a right-only pointer finds the first suffix start that can follow it, and every later start works too
LeetCode 2122: Recover the Original Array
·3 mins
LeetCode Hard Two-Pointers Sorting Hash-Table Enumeration Array
LeetCode 2122 note: the smallest number is always in lower, so enumerating its partner fixes k; after sorting, two pointers pair each smallest unmatched number with the number 2k above it
LeetCode 1984: Minimum Difference Between Highest and Lowest of K Scores
·1 min
LeetCode Easy Sliding-Window Sorting Array
LeetCode 1984 note: after sorting, the chosen k scores are always contiguous; slide a fixed window of length k and subtract its ends
LeetCode 658: Find K Closest Elements
·3 mins
LeetCode Medium Two-Pointers Binary-Search Sliding-Window
LeetCode 658 note: shrink two pointers from both ends to length k, or binary search the window start
LeetCode 633: Sum of Square Numbers
·2 mins
LeetCode Medium Two-Pointers Math Binary-Search
LeetCode 633 note: converge a from 0 and b from isqrt(c); shrink b when the sum is too big and grow a when it is too small; watch for 0, a == b, and floating-point square roots
LeetCode 3820: Pythagorean Distance Nodes in a Tree
·2 mins
LeetCode Medium Tree Bfs Graph
LeetCode 3820 note: three BFS runs give every node’s distance to the three targets