Binary Search

March 18, 2026 · 0 min · map[name:Jeanphilo]

LeetCode 198: House Robber, Deriving 1D DP from Rob or Skip

Problem Input and Output Input: an integer array nums nums[i] is the money in the i-th house Adjacent houses cannot both be robbed Output: return the maximum amount of money that can be robbed Constraints: 1 <= nums.length <= 100, 0 <= nums[i] <= 400 Examples Input: nums = [1,2,3,1] Output: 4 Explanation: rob indices 0 and 2, for 1 + 3 = 4 Input: nums = [2,7,9,3,1] Output: 12 Explanation: rob indices 0, 2, and 4, for 2 + 9 + 1 = 12 This article uses Python only and derives 1D DP from the conflict between two choices. ...

May 3, 2026 · 6 min · map[name:Jeanphilo]

LeetCode 70: Climbing Stairs, Deriving 1D DP from dp[i]

Problem Input and Output Input: an integer n Meaning: climb to the top of the n-step staircase Each move can climb 1 or 2 steps Output: return the number of distinct ways to reach the top Constraints: 1 <= n <= 45 Examples Input: n = 2 Output: 2 Explanation: 1+1, 2 Input: n = 3 Output: 3 Explanation: 1+1+1, 1+2, 2+1 This article uses Python only and derives the final solution step by step from the meaning of dp[i]. ...

May 3, 2026 · 6 min · map[name:Jeanphilo]

LeetCode 746: Min Cost Climbing Stairs, Deriving DP from the Top Position

Problem Input and Output Input: an integer array cost cost[i] is the cost of stepping on stair i Each move can climb 1 or 2 steps You may start from index 0 or index 1 Output: return the minimum cost to reach the top Constraints: 2 <= cost.length <= 1000, 0 <= cost[i] <= 999 Examples Input: cost = [10,15,20] Output: 15 Explanation: start from index 1, pay 15, then reach the top directly Input: cost = [1,100,1,1,1,100,1,1,100,1] Output: 6 Start from the Top Position of [10,15,20] Look at the small example: ...

May 3, 2026 · 6 min · map[name:Jeanphilo]

LeetCode 78: Subsets, Derive the startIndex Backtracking Template

Problem Requirement Input / Output Input: nums, with 1 <= nums.length <= 10 Value range: -10 <= nums[i] <= 10 All elements in nums are distinct Output: return all possible subsets of nums Ordering: the result order does not matter, and subset element order is not the point Example Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] This tutorial builds one minimal Python solution. Start From the Search Tree for [1,2] The smallest branching example is: ...

May 3, 2026 · 6 min · map[name:Jeanphilo]

LeetCode 90: Subsets II, Derive Layer-Level Deduplication

Problem Requirement Input / Output Input: nums, with 1 <= nums.length <= 10 Value range: -10 <= nums[i] <= 10 nums may contain duplicates Output: return all unique subsets Ordering: the result order does not matter, but the same value sequence may appear only once Example Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] This tutorial starts with a correct but wasteful version, then derives sorting plus layer-level deduplication. Start From Duplicate Branches in [1,2,2] The smallest example that exposes the issue is: ...

May 3, 2026 · 6 min · map[name:Jeanphilo]

Hot100: Binary Tree Maximum Path Sum (Tree DP / Single-Side Gain ACERS Guide)

Subtitle / Summary The easiest way to get lost in LeetCode 124 is to make one recursive return value carry too much meaning. The stable design is to separate two roles: the recursion returns only the best single-side gain to the parent, while the full path through the current node is used to update the global maximum. Reading time: 12-15 min Tags: Hot100, binary tree, tree DP, DFS, postorder SEO keywords: Binary Tree Maximum Path Sum, tree DP, single-side gain, postorder, DFS, LeetCode 124 Meta description: Learn LeetCode 124 from the single-side gain invariant, global path update, and negative-branch pruning, with runnable multi-language implementations. A — Algorithm Problem Restatement A path in a binary tree is a sequence of nodes such that: ...

April 20, 2026 · 13 min · map[name:Jeanphilo]

Hot100: Binary Tree Right Side View (Level Order Last-Node Rule ACERS Guide)

Subtitle / Summary LeetCode 199 is not really about visual imagination. It is about translating a viewpoint problem into a level problem. Once you realize that the right side view is simply the last node of each level, the problem becomes a standard breadth-first traversal. Reading time: 10-13 min Tags: Hot100, binary tree, BFS, level order, queue SEO keywords: Binary Tree Right Side View, BFS, level order, queue, right-first DFS, LeetCode 199 Meta description: Learn LeetCode 199 from the core equivalence “right side view = last node of each level”, with step-by-step derivation, engineering mappings, and runnable multi-language implementations. A — Algorithm Problem Restatement Given the root root of a binary tree, imagine standing on its right side and return the values of the nodes you can see from top to bottom. ...

April 20, 2026 · 12 min · map[name:Jeanphilo]

Hot100: Construct Binary Tree from Preorder and Inorder Traversal (Indexed Divide-and-Conquer ACERS Guide)

Subtitle / Summary The key to LeetCode 105 is not memorizing that preorder and inorder can reconstruct a tree. It is understanding what each traversal contributes: preorder tells you the root, inorder tells you the left/right boundary. Once those roles are clear, the whole problem becomes a clean indexed divide-and-conquer. Reading time: 12-15 min Tags: Hot100, binary tree, divide and conquer, hash map, preorder SEO keywords: Construct Binary Tree from Preorder and Inorder Traversal, preorder, inorder, divide and conquer, hash map, LeetCode 105 Meta description: Learn LeetCode 105 from traversal roles, indexed recursion, and hash-map root lookup, with runnable multi-language implementations. A — Algorithm Problem Restatement Given the preorder traversal preorder and inorder traversal inorder of a binary tree, reconstruct the tree and return its root. ...

April 20, 2026 · 13 min · map[name:Jeanphilo]

Hot100: Convert Sorted Array to Binary Search Tree (Midpoint Divide-and-Conquer ACERS Guide)

Subtitle / Summary The key to LeetCode 108 is not recursion by itself. It is noticing that the problem wants two things at the same time: BST ordering and height balance. Once you read those two constraints together, “pick the middle element as the root” stops being a trick and becomes the natural construction rule. Reading time: 11-14 min Tags: Hot100, binary tree, BST, divide and conquer, recursion SEO keywords: Convert Sorted Array to Binary Search Tree, BST, balanced BST, divide and conquer, recursion, LeetCode 108 Meta description: Learn LeetCode 108 from the midpoint-construction idea, with step-by-step derivation, engineering mappings, and runnable multi-language implementations. A — Algorithm Problem Restatement Given an integer array nums sorted in strictly increasing order, convert it into a height-balanced binary search tree. ...

April 20, 2026 · 12 min · map[name:Jeanphilo]

Hot100: Flatten Binary Tree to Linked List (Reverse Preorder Rewiring ACERS Guide)

Subtitle / Summary The real difficulty of LeetCode 114 is not “flattening” a tree. It is rewiring pointers without destroying the structure you still need. Once you notice that the final linked list must follow preorder order, reverse preorder with a prev pointer becomes a very stable in-place solution. Reading time: 12-15 min Tags: Hot100, binary tree, preorder, in-place, recursion SEO keywords: Flatten Binary Tree to Linked List, preorder, in-place, reverse preorder, LeetCode 114 Meta description: Learn LeetCode 114 from preorder order and reverse-preorder rewiring, with step-by-step derivation, engineering mappings, and runnable multi-language implementations. A — Algorithm Problem Restatement Given the root root of a binary tree, flatten the tree into a linked list in place. ...

April 20, 2026 · 12 min · map[name:Jeanphilo]