Medium
LeetCode 1186: Maximum Subarray Sum with One Deletion
·2 mins
LeetCode
Medium
Array
Dynamic-Programming
LeetCode 1186 note: Kadane with one more state, ending here with no deletion or with one deletion; update the answer at every step
LeetCode 775: Global and Local Inversions
·3 mins
LeetCode
Medium
Array
Math
LeetCode 775 note: turn a counting problem into an existence check and solve it in O(n)
LeetCode 912: Sort an Array
·3 mins
LeetCode
Medium
Sorting
Merge-Sort
Divide-and-Conquer
LeetCode 912 note: hand-written merge sort and the two bugs I hit
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 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 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 4045: Count Robot Groups
·3 mins
LeetCode
Medium
Stack
Monotonic-Stack
Greedy
LeetCode 4045 note: merge robots already close at t = 0, then go right to left like car fleets, where anything faster than the fleet ahead eventually joins it
LeetCode 622: Design Circular Queue
·2 mins
LeetCode
Medium
Queue
Design
Array
LeetCode 622 note: a fixed array plus head and count, every position taken modulo the capacity, and count deciding empty versus full
LeetCode 4019: Merge Close Characters II
·2 mins
LeetCode
Medium
Hash-Table
String
LeetCode 4019 note: kept characters never change again, so record each character’s last position in the result; a new character within k of it gets merged away
LeetCode 323: Number of Connected Components in an Undirected Graph
·2 mins
LeetCode
Medium
Graph
Dfs
Bfs
Union-Find
LeetCode 323 note: count components with DFS or Union-Find; Union-Find without path compression is much slower
LeetCode 4040: Minimum Operations to Form Subset Sum I
·2 mins
LeetCode
Medium
Dynamic-Programming
Knapsack
Array
LeetCode 4040 note: with multiplications before divisions, the best is always multiply-only or divide-only; list each number’s reachable values and costs, then run a grouped 0/1 knapsack