↓Skip to main content

Hard

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 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
LeetCode 3313: Find the Last Marked Nodes in Tree
·2 mins
LeetCode Hard Tree Bfs Graph Tree-Diameter
LeetCode 3313 note: the last marked node is the farthest one, and a farthest node can always be a diameter endpoint
LeetCode 2608: Shortest Cycle in a Graph
·3 mins
LeetCode Hard Bfs Graph
LeetCode 2608 note: BFS from every node; a visited neighbor that is not the parent closes a cycle of length dist[u] + dist[v] + 1
LeetCode 2561: Rearranging Fruits
·2 mins
LeetCode Hard Greedy Hash-Table Sorting
LeetCode 2561 note: every value’s total must be even; sort the excess, and each cheap half item swaps with an expensive one at min(its value, 2 × global minimum)
LeetCode 716: Max Stack
·3 mins
LeetCode Hard Stack Heap Design Lazy-Deletion
LeetCode 716 note: store unique ids on both the stack and the heap, record deletions by id, and discard deleted entries lazily; tracking values cannot tell duplicates apart
LeetCode 2218: Maximum Value of K Coins From Piles
·2 mins
LeetCode Hard Dynamic-Programming Knapsack Prefix-Sum Array
LeetCode 2218 note: piles are taken from the top, so taking x coins is worth a prefix sum; a grouped knapsack with dp[j] = the best value taking j coins so far, enumerating how many from each pile
LeetCode 2416: Sum of Prefix Scores of Strings
·2 mins
LeetCode Hard Trie String Counting
LeetCode 2416 note: each Trie node counts the words passing through it, and a word’s answer is the sum of counts along its path
LeetCode 987: Vertical Order Traversal of a Binary Tree
·2 mins
LeetCode Hard Tree Binary-Tree Bfs Sorting
LeetCode 987 note: collect (col, row, val) triples, sort them, and group by column
LeetCode 940: Distinct Subsequences II
·2 mins
LeetCode Hard Dynamic-Programming String
LeetCode 940 note: group by last character; distinct subsequences ending with c = every current distinct subsequence with c appended, plus c itself, replacing the old value
LeetCode 2973: Find Number of Coins to Place in Tree Nodes
·3 mins
LeetCode Hard Dynamic-Programming Tree-Dp Depth-First-Search Sorting Heap Tree
LeetCode 2973 note: the max product of three is either the three largest or the two smallest times the largest, so each subtree returns only its 2 smallest and 3 largest values, merged in post-order
LeetCode 1246: Palindrome Removal
·2 mins
LeetCode Hard Dynamic-Programming Interval-Dp Array
LeetCode 1246 note: interval DP taking the minimum of removing arr[i] alone, removing equal ends together with the inside, or splitting into two parts