↓Skip to main content

LeetCode

LeetCode 253: Meeting Rooms II
·2 mins
LeetCode Medium Heap Sorting Intervals Sweep-Line
LeetCode 253 note: a heap of end times for running meetings; half-open intervals need <=
LeetCode 1220: Count Vowels Permutation
·2 mins
LeetCode Hard Dynamic-Programming State-Machine
LeetCode 1220 note: state machine DP on the last vowel; turn “what may follow x” around into “what may precede x”
LeetCode 4081: Prime Subset Selection II
·4 mins
LeetCode Hard Knapsack-Dp Number-Theory
LeetCode 4081 note: sieve the primes, then 0/1 knapsack with dp[j] the fewest primes summing to j; adding primes from large to small, a tie gives a smaller first element, so updating on <= yields the lexicographically smallest
LeetCode 2751: Robot Collisions
·3 mins
LeetCode Hard Stack Simulation Sorting
LeetCode 2751 note: sort by position, push right-moving robots, and let each left-moving robot collide until one side is gone
LeetCode 2584: Split the Array to Make Coprime Products
·3 mins
LeetCode Hard Math Number-Theory Sieve Hash-Table Array
LeetCode 2584 note: coprime products ⇔ no prime factor in both parts; record each prime’s last index, extend reach left to right, and split after i when reach == i
LeetCode 2267: Check if There Is a Valid Parentheses String Path
·3 mins
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Bracket-Sequences
LeetCode 2267 note: the state is (r, c, open balance); the balance can be neither negative nor more than the remaining steps; search the states with BFS / DP and succeed at the end with balance 0
LeetCode 2761: Prime Pairs With Target Sum
·2 mins
LeetCode Medium Math Number-Theory Sieve Enumeration Array
LeetCode 2761 note: sieve primes below n, then try x up to n / 2; when x and n - x are both prime, they form a pair
LeetCode 2523: Closest Prime Numbers in Range
·2 mins
LeetCode Medium Math Number-Theory Sieve Array
LeetCode 2523 note: sieve the primes in range, since only adjacent primes can be closest; or trial-divide rightward from left and stop at the first twin primes with gap 2
LeetCode 1998: GCD Sort of an Array
·3 mins
LeetCode Hard Math Number-Theory Union-Find Sieve Sorting Array
LeetCode 1998 note: numbers in one component can be permuted freely; union each number with its prime factors via SPF, and the array sorts iff each position’s old and new values share a group
LeetCode 1001: Grid Illumination
·3 mins
LeetCode Hard Hash-Table Array
LeetCode 1001 note: one counter each for rows, columns and both diagonals (r - c, r + c), plus a set of lit lamps; turning one off decrements all four
LeetCode 4067: Longest Subarray With Restricted Pair Sums
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 4067 note: subarrays of a valid window stay valid, so slide; adding x only needs checks involving x: x as an addend (x + y in the window) or x as the sum (x - y in the window)
LeetCode 1842: Next Palindrome Using Same Digits
·3 mins
LeetCode Hard Two-Pointers String
LeetCode 1842 note: a palindrome is determined by its left half and the middle digit stays put; take the next permutation of the left half and mirror it