Coding Interview
1143. Longest Common Subsequence
Jason Yang · 04 Aug, 2026
Longest Common Subsequence (LeetCode 1143): the match-or-drop grid recurrence, why the DP table is 2D, and a rolling O(min(m,n))-space solution in Java.
Showing Posts From
Coding Interview
Jason Yang · 04 Aug, 2026
Longest Common Subsequence (LeetCode 1143): the match-or-drop grid recurrence, why the DP table is 2D, and a rolling O(min(m,n))-space solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Palindromic Substrings (LeetCode 647): count every palindrome by expanding around each center for O(n^2) time and O(1) space in Java, plus the DP alternative.
Coding Interview
Jason Yang · 04 Aug, 2026
Subtree of Another Tree (LeetCode 572): anchor the match at every node and reuse Same Tree as the equality check — the clean O(m*n) recursion in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Non-overlapping Intervals (LeetCode 435): sort by end time and greedily keep the earliest-finishing intervals, so the fewest removals fall out for free.
Coding Interview
Jason Yang · 04 Aug, 2026
Longest Repeating Character Replacement (LeetCode 424): why a window is valid when its length minus its most-frequent letter stays within k, solved in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Pacific Atlantic Water Flow (LeetCode 417): flip the flow and DFS inward from each ocean's border, then intersect the two reachable sets. Java solution.
Coding Interview
Jason Yang · 04 Aug, 2026
Sum of Two Integers (LeetCode 371): add two numbers using only XOR and AND. Here's why XOR is the sum, AND is the carry, and how the loop lands in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Top K Frequent Elements (LeetCode 347): count with a hash map, then beat the O(n log n) bound by bucketing values by frequency for an O(n) answer in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Counting Bits (LeetCode 338): why dp[i] = dp[i >> 1] + (i & 1) counts set bits in one linear pass, plus the built-in baseline it beats — in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Number of Connected Components (LeetCode 323) in Java: count merges instead of nodes with Union-Find, or flood-fill each unvisited node with DFS.
Coding Interview
Jason Yang · 04 Aug, 2026
Coin Change (LeetCode 322): why grabbing the biggest coin fails, and the bottom-up DP that finds the fewest coins for an amount in O(amount × coins) — in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Longest Increasing Subsequence (LeetCode 300): the O(n^2) DP for the intuition, then the O(n log n) patience-sorting trick with binary search, in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Serialize and Deserialize Binary Tree (LeetCode 297): why a preorder walk with explicit null markers round-trips any tree, coded cleanly in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Find Median from Data Stream (LeetCode 295): balance a max-heap and a min-heap so the median sits at the top, with O(log n) inserts in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Encode and Decode Strings (LeetCode 271): why a delimiter alone can't work, and the length-prefix codec that round-trips any characters in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Alien Dictionary (LeetCode 269): turn a sorted word list into a character graph, then topologically sort it with Kahn's BFS in Java — cycles and prefixes included.
Coding Interview
Jason Yang · 04 Aug, 2026
Missing Number (LeetCode 268): why XOR-ing every index with every value leaves exactly the missing one, plus the Gauss-sum alternative, in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Graph Valid Tree (LeetCode 261): a tree is a connected, acyclic graph with exactly n-1 edges — solve it with union-find cycle detection in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Meeting Rooms II (LeetCode 253): find the minimum rooms by tracking peak overlap — the min-heap solution and the sweep-line trick, both in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Meeting Rooms (LeetCode 252): sort intervals by start, then one pass reveals whether any two overlap. Java code, the strict-vs-equal trap, and complexity.
Coding Interview
Jason Yang · 04 Aug, 2026
Valid Anagram (LeetCode 242): why a 26-slot frequency count beats sorting, the one-array Java solution, and how the Unicode follow-up changes the answer.
Coding Interview
Jason Yang · 04 Aug, 2026
Lowest Common Ancestor of a BST (LeetCode 235): use the sorted-order property to walk down until the two targets split, an O(h) one-pass solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Kth Smallest Element in a BST (LeetCode 230): why an inorder walk visits values in sorted order, so the kth node you touch is the answer — solved in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Invert Binary Tree (LeetCode 226): swap every node's children with a three-line recursion, plus the iterative BFS version and the null base case that matters.
Coding Interview
Jason Yang · 04 Aug, 2026
Word Search II (LeetCode 212): why one shared Trie beats running Word Search once per word, plus the backtracking DFS and pruning tricks, in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Design Add and Search Words (LeetCode 211): store words in a trie, then DFS through it so a '.' wildcard can branch into every child. Java solution explained.
Coding Interview
Jason Yang · 04 Aug, 2026
Implement Trie (LeetCode 208): build a prefix tree in Java where each node branches 26 ways, so insert, search, and startsWith all run in O(word length).
Coding Interview
Jason Yang · 04 Aug, 2026
Course Schedule (LeetCode 207): the whole problem is 'does this directed graph have a cycle?' Solve it with Kahn's topological sort in Java, plus the DFS alternative.
Coding Interview
Jason Yang · 04 Aug, 2026
Reverse Linked List (LeetCode 206): the three-pointer flip that reverses a singly linked list in place, plus the recursive version and why order matters.
Coding Interview
Jason Yang · 04 Aug, 2026
Number of Islands (LeetCode 200): count connected land groups with a DFS flood fill that sinks each island as it's found, in clean idiomatic Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Number of 1 Bits (LeetCode 191): counting set bits with a plain shift loop, then Brian Kernighan's n & (n-1) trick that only loops once per set bit — in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Reverse Bits (LeetCode 190): peel the low bit, stack it onto the result 32 times, and the >>> vs >> shift nuance in Java. Plus the byte-cache follow-up.
Coding Interview
Jason Yang · 04 Aug, 2026
Reorder List (LeetCode 143): interleave a linked list front-to-back in O(1) space by combining three classic pointer moves — find the middle, reverse, merge.
Coding Interview
Jason Yang · 04 Aug, 2026
Linked List Cycle (LeetCode 141): why two pointers moving at different speeds must collide inside a loop, and the O(1)-space Floyd's algorithm in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Word Break (LeetCode 139): why a greedy longest-match fails, how prefix DP with dp[i] = 'is s[0..i) segmentable' fixes it, and the prefix-DP Java solution.
Coding Interview
Jason Yang · 04 Aug, 2026
Clone Graph (LeetCode 133): why a single HashMap from original to copy solves cycles and shared neighbors at once, with a clean DFS solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Binary Tree Maximum Path Sum (LeetCode 124): why one DFS returns a single-branch gain to the parent while tracking a global best that bends through a node.
Coding Interview
Jason Yang · 04 Aug, 2026
Construct Binary Tree from Preorder and Inorder Traversal (LeetCode 105): why preorder hands you the root and inorder splits left from right, in O(n) Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Maximum Depth of Binary Tree (LeetCode 104): the one-line DFS recurrence, why depth is 1 + the taller subtree, and a BFS alternative in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Binary Tree Level Order Traversal (LeetCode 102): the queue-based BFS, why snapshotting the queue size groups each level, and clean Java code.
Coding Interview
Jason Yang · 04 Aug, 2026
Same Tree (LeetCode 100): compare two binary trees for identical shape and values with a four-line recursion, plus the iterative queue version and its traps.
Coding Interview
Jason Yang · 04 Aug, 2026
Validate Binary Search Tree (LeetCode 98): why checking a node against its children fails, and the min/max bounds recursion that fixes it in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Decode Ways (LeetCode 91): the Fibonacci-shaped count recurrence, why leading zeros kill a decoding, and the clean O(1)-space DP in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Word Search (LeetCode 79): how DFS plus in-place marking walks the grid, why you restore each cell on the way out, and the clean Java backtracking solution.
Coding Interview
Jason Yang · 04 Aug, 2026
Set Matrix Zeroes (LeetCode 73): why an in-place mark-then-sweep needs the matrix's own first row and column as scratch, plus the O(1)-space Java code.
Coding Interview
Jason Yang · 04 Aug, 2026
Unique Paths (LeetCode 62): why each grid cell is the sum of the ways from above and from the left, plus the O(n)-space rolling DP solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Insert Interval (LeetCode 57): why an already-sorted list lets you insert in one linear pass — the before / merge / after sweep in Java, no sorting needed.
Coding Interview
Jason Yang · 04 Aug, 2026
Merge Intervals (LeetCode 56): sort by start, then sweep once and extend the last interval whenever the next one overlaps — the Java sort-and-merge template.
Coding Interview
Jason Yang · 04 Aug, 2026
Jump Game (LeetCode 55): why one greedy pass tracking the farthest reachable index beats the O(n^2) DP, and why a trailing zero is the real trap, in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Spiral Matrix (LeetCode 54): walk the grid clockwise by tracking four edges and peeling one row or column off after each pass — clean Java, no visited set.
Coding Interview
Jason Yang · 04 Aug, 2026
Group Anagrams (LeetCode 49): pick a canonical key so every anagram hashes to the same bucket. Sorted-key vs count-array key in Java, with complexity.
Coding Interview
Jason Yang · 04 Aug, 2026
Rotate Image (LeetCode 48): why transpose-then-reverse rotates a matrix 90° clockwise in place, with the O(1)-space Java solution and the index math behind it.
Coding Interview
Jason Yang · 04 Aug, 2026
Combination Sum (LeetCode 39): why passing the same start index lets you reuse numbers, plus a sorted-and-pruned backtracking solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Merge k Sorted Lists (LeetCode 23): why merging one list at a time is O(n·k), and how a min-heap or pairwise merging cuts it to O(n log k) in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Merge Two Sorted Lists (LeetCode 21): the dummy-head trick that removes every empty-list special case, plus the O(1)-space splice solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Valid Parentheses (LeetCode 20): why a stack is the natural fit for matching brackets, a clean Java solution, and the empty-stack edge cases interviewers probe.
Coding Interview
Jason Yang · 04 Aug, 2026
Remove Nth Node From End of List (LeetCode 19): why a two-pointer gap lets you delete the nth-from-last node in one pass, plus the dummy-head trick in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Longest Palindromic Substring (LeetCode 5): why expanding around each center beats the DP table, the two-center trick for even lengths, and clean Java.
Coding Interview
Jason Yang · 04 Aug, 2026
House Robber II (LeetCode 213): the circular twist solved by running the linear House Robber twice — once excluding the first house, once the last — in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
House Robber (LeetCode 198): the take-or-skip recurrence, why you can't rob adjacent houses, and the O(1)-space rolling solution in Java.
Coding Interview
Jason Yang · 04 Aug, 2026
Climbing Stairs (LeetCode 70): why the answer is Fibonacci, the memoized and bottom-up solutions in Java, and the rolling trick that drops it to O(1) space.
Coding Interview
Jason Yang · 31 Mar, 2026
Minimum Window Substring (LeetCode 76): the sliding-window O(n) approach with character counts to shrink to the smallest valid window, with a visual.
Coding Interview
Jason Yang · 31 Mar, 2026
Longest Substring Without Repeating Characters (LeetCode 3): the sliding-window O(n) approach, and why the int[128] version can jump the left pointer instead of stepping — with interactive visuals.
Coding Interview
Jason Yang · 31 Mar, 2026
Valid Palindrome (LeetCode 125): the two-pointer O(n) check that skips non-alphanumerics in place, why it beats building a cleaned string, and the edge cases.
Coding Interview
Jason Yang · 30 Mar, 2026
Longest Consecutive Sequence (LeetCode 128): the hash-set O(n) trick that only counts from sequence starts, with an interactive step-by-step visual.
Coding Interview
Jason Yang · 27 Mar, 2026
3Sum (LeetCode 15): sort then two-pointer for an O(n^2) solution, why sorting unlocks it, and the three places duplicate triplets sneak in — with an interactive visualization.
Coding Interview
Jason Yang · 27 Mar, 2026
Container With Most Water (LeetCode 11): the two-pointer O(n) approach, and the short proof for why moving the shorter line never skips a better answer — with an interactive visualization.
Coding Interview
Jason Yang · 03 Dec, 2025
Search in Rotated Sorted Array (LeetCode 33): a modified O(log n) binary search that finds which half is sorted, with an interactive step visual.
Coding Interview
Jason Yang · 02 Dec, 2025
Find Minimum in Rotated Sorted Array (LeetCode 153): the O(log n) binary search that locates the rotation pivot, explained step by step with examples.
Coding Interview
Jason Yang · 01 Dec, 2025
Maximum Product Subarray (LeetCode 152): why you track max and min together to handle negatives and zeros, and the O(n) dynamic-programming solution.
Coding Interview
Jason Yang · 29 Nov, 2025
Maximum Subarray (LeetCode 53): Kadane's algorithm explained as one decision — extend or restart — plus the O(n) to O(1) space drop and a full worked trace.
Coding Interview
Jason Yang · 28 Nov, 2025
Product of Array Except Self (LeetCode 238): the prefix and suffix product trick that avoids division for an O(n) solution, with an interactive visual.
Coding Interview
Jason Yang · 27 Nov, 2025
Contains Duplicate (LeetCode 217): compare the brute-force, sorting, and hash-set approaches — and why 'just use a hash set' isn't always the right reflex.
Coding Interview
Jason Yang · 26 Nov, 2025
Best Time to Buy and Sell Stock (LeetCode 121): the single-pass O(n) solution that tracks the lowest price so far, with an interactive step-by-step visual.
Coding Interview
Jason Yang · 11 Nov, 2025
Two Sum (LeetCode 1) two ways: the brute-force O(n^2) scan vs the hash-map O(n) approach — with the intuition, edge cases, and the space-time trade-off.