Hard
LeetCode 1210: Minimum Moves to Reach Target with Rotations
·3 mins
LeetCode
Hard
Bfs
Matrix
LeetCode 1210 note: the BFS state is (tail position, orientation), each move checks its own empty cells, and equal step costs need no heap
LeetCode 1028: Recover a Tree From Preorder Traversal
·3 mins
LeetCode
Hard
Tree
Binary-Tree
Dfs
Stack
String
LeetCode 1028 note: a stack holds the path from the root to the current node, so its length is the depth
LeetCode 4046: Minimum Cost Path With At Most K Turns
·3 mins
LeetCode
Hard
Graph
Shortest-Path
Heap
Matrix
LeetCode 4046 note: Dijkstra with the last direction and the turns used in the state; for the same cell and direction, an earlier pop is cheaper, and if it also used fewer turns it dominates later states
LeetCode 2509: Cycle Length Queries in a Tree
·2 mins
LeetCode
Hard
Tree
Binary-Tree
Lca
LeetCode 2509 note: in heap numbering the parent is v // 2, so climb the larger node until both meet at the LCA
LeetCode 301: Remove Invalid Parentheses
·4 mins
LeetCode
Hard
Backtracking
Bfs
String
LeetCode 301 note: compute the minimum left and right deletions before backtracking, or BFS one deletion per level and stop at the first valid level
LeetCode 1425: Constrained Subsequence Sum
·2 mins
LeetCode
Hard
Sliding-Window
Dynamic-Programming
Monotonic-Queue
Heap
Queue
Array
LeetCode 1425 note: dp[i] = nums[i] + max(0, max of the previous k dp values); that max is a sliding window maximum, available in O(1) from a monotonic deque
LeetCode 1373: Maximum Sum BST in Binary Tree
·2 mins
LeetCode
Hard
Dynamic-Programming
Tree-Dp
Binary-Search-Tree
Depth-First-Search
Tree
LeetCode 1373 note: a post-order traversal returns (sum, min, max, is_bst); a node roots a BST when the left max < its value < the right min
LeetCode 996: Number of Squareful Arrays
·3 mins
LeetCode
Hard
Dynamic-Programming
Bitmask
Backtracking
Math
Hash-Table
LeetCode 996 note: with n at most 12, use bitmask DP: dp[mask][last] = permutations using the set mask and ending with last; dedupe by using equal values in index order
LeetCode 773: Sliding Puzzle
·2 mins
LeetCode
Hard
Breadth-First-Search
Dynamic-Programming
Memoization
Matrix
LeetCode 773 note: there are only 6! = 720 boards, so flatten each board into a string as a BFS state, with swaps of 0 and a neighbor as the moves
LeetCode 1354: Construct Target Array With Multiple Sums
·3 mins
LeetCode
Hard
Heap
Math
Greedy
LeetCode 1354 note: work backwards, since the maximum was set last; jump with a modulo when the gap is large
LeetCode 719: Find K-th Smallest Pair Distance
·2 mins
LeetCode
Hard
Binary-Search
Two-Pointers
Sliding-Window
Sorting
LeetCode 719 note: binary search on the distance, counting pairs with distance <= mid using a sliding window
LeetCode 499: The Maze III
·3 mins
LeetCode
Hard
Graph
Dijkstra
Shortest-Path
Heap
Matrix
LeetCode 499 note: Dijkstra prioritized by (distance, path string), and the ball stops when it rolls over the hole