Skip to content

Commit 76ea144

Browse files
committed
Nothing missed: 37 more pages — the company/niche folders (google, microsoft, facebook, geo, stock_market, etc.) — Floyd-Warshall, MK Average, Parallel Courses II, Great Town Split, Min Ticket Price, Largest Time, Quad Tree, Shortest Common Supersequence, Climbing Stairs, Interval Intersections, Plus One, Reverse Int, Toeplitz, Transpose, Missing Ranges, Rect Area/Overlap, Zero Array, Odd Even LL, Rotate List, Inorder Successor, House Robber III, Sliding Puzzle, Shortest Bridge, 132 Pattern, Maximal Rectangle, Valid Parens Check, Goat Latin, Detect Capital, Is Subsequence, String Compression, Custom Sort, Rank Teams, Contiguous Array, Set Mismatch, Detect Squares, Snapshot Array, Car Pooling, Meeting Scheduler, Count Collisions, Partition Labels, Break Palindrome, best-time I/III/cooldown/fee
1 parent 0e1d32b commit 76ea144

58 files changed

Lines changed: 9945 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CodingInterviewFightClub/src/SUMMARY.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
- [2.25 Cherry Pickup](ch02-dynamic-programming/cherry-pickup.md)
6565
- [2.26 Racecar](ch02-dynamic-programming/racecar.md)
6666
- [2.27 Minimum Number Of Taps To Water The Garden](ch02-dynamic-programming/minimum-number-of-taps.md)
67+
- [2.28 Shortest Common Supersequence](ch02-dynamic-programming/shortest-common-supersequence.md)
68+
- [2.29 Min Cost Climbing Stairs](ch02-dynamic-programming/min-cost-climbing-stairs.md)
6769

6870
- [3. Arrays, Two Pointers & Matrices](ch03-arrays/index.md)
6971
- [3.0 Pattern Primer: Two Pointers & The Sorted-Array Dance](ch03-arrays/pattern-primer.md)
@@ -90,6 +92,15 @@
9092
- [3.21 Increasing Triplet Subsequence](ch03-arrays/increasing-triplet-subsequence.md)
9193
- [3.22 Diagonal Traverse](ch03-arrays/diagonal-traverse.md)
9294
- [3.23 Find The Highest Altitude](ch03-arrays/find-the-highest-altitude.md)
95+
- [3.24 Interval List Intersections](ch03-arrays/interval-list-intersections.md)
96+
- [3.25 Plus One](ch03-arrays/plus-one.md)
97+
- [3.26 Reverse Integer](ch03-arrays/reverse-integer.md)
98+
- [3.27 Toeplitz Matrix](ch03-arrays/toeplitz-matrix.md)
99+
- [3.28 Transpose Matrix](ch03-arrays/transpose-matrix.md)
100+
- [3.29 Missing Ranges](ch03-arrays/missing-ranges.md)
101+
- [3.30 Rectangle Area](ch03-arrays/rectangle-area.md)
102+
- [3.31 Rectangle Overlap](ch03-arrays/rectangle-overlap.md)
103+
- [3.32 Zero Array Transformation](ch03-arrays/zero-array-transformation.md)
93104
- [2.12 Closest Subsequence Sum](ch02-dynamic-programming/closest-subsequence-sum.md)
94105

95106
- [4. Linked Lists](ch04-linked-lists/index.md)
@@ -108,6 +119,8 @@
108119
- [4.12 Reverse Nodes In K Groups](ch04-linked-lists/reverse-nodes-in-k-groups.md)
109120
- [4.13 Insert Into A Sorted Circular Linked List](ch04-linked-lists/insert-into-a-sorted-circular-linked-list.md)
110121
- [4.14 Maximum Twin Sum Of A Linked List](ch04-linked-lists/maximum-twin-sum.md)
122+
- [4.15 Odd Even Linked List](ch04-linked-lists/odd-even-linked-list.md)
123+
- [4.16 Rotate List](ch04-linked-lists/rotate-list.md)
111124

112125
- [5. Trees](ch05-trees/index.md)
113126
- [5.0 Pattern Primer: The Recursive Data Structure](ch05-trees/pattern-primer.md)
@@ -133,6 +146,9 @@
133146
- [5.20 Range Sum Of BST](ch05-trees/range-sum-of-bst.md)
134147
- [5.21 Longest Univalue Path](ch05-trees/longest-univalue-path.md)
135148
- [5.22 Leaf-Similar Trees](ch05-trees/leaf-similar-trees.md)
149+
- [5.23 Construct Quad Tree](ch05-trees/construct-quad-tree.md)
150+
- [5.24 Inorder Successor In BST](ch05-trees/inorder-successor.md)
151+
- [5.25 House Robber III](ch05-trees/house-robber-iii.md)
136152

137153
- [6. Graphs](ch06-graphs/index.md)
138154
- [6.0 Pattern Primer: The Seven Engines](ch06-graphs/pattern-primer.md)
@@ -159,6 +175,9 @@
159175
- [6.21 Number Of Islands II](ch06-graphs/number-of-islands-ii.md)
160176
- [6.22 Island Perimeter](ch06-graphs/island-perimeter.md)
161177
- [6.23 N-Coloring Greedy](ch06-graphs/n-coloring-greedy.md)
178+
- [6.24 Parallel Courses II](ch06-graphs/parallel-courses-ii.md)
179+
- [6.25 Sliding Puzzle](ch06-graphs/sliding-puzzle.md)
180+
- [6.26 Shortest Bridge](ch06-graphs/shortest-bridge.md)
162181

163182
- [7. Heaps & Priority Queues](ch07-heaps/index.md)
164183
- [7.0 Pattern Primer: The Lazy Sorted Structure](ch07-heaps/pattern-primer.md)
@@ -174,6 +193,7 @@
174193
- [7.10 Longest Happy String](ch07-heaps/longest-happy-string.md)
175194
- [7.11 Merge K Sorted Lists](ch07-heaps/merge-k-sorted-lists.md)
176195
- [7.12 Find Score Of An Array After Marking All Elements](ch07-heaps/find-score-of-an-array-after-marking.md)
196+
- [7.13 Finding MK Average](ch07-heaps/finding-mk-average.md)
177197

178198
- [8. Stacks & Queues](ch08-stacks/index.md)
179199
- [8.0 Pattern Primer: LIFO, FIFO, and the Monotonic Stack](ch08-stacks/pattern-primer.md)
@@ -196,6 +216,9 @@
196216
- [8.17 Minimum Add To Make Parentheses Valid](ch08-stacks/minimum-add-to-make-parentheses-valid.md)
197217
- [8.18 Minimum Remove To Make Valid](ch08-stacks/minimum-remove-to-make-valid-parentheses.md)
198218
- [8.19 Remove Duplicate Letters](ch08-stacks/remove-duplicate-letters.md)
219+
- [8.20 One Three Two Pattern](ch08-stacks/one-three-two-pattern.md)
220+
- [8.21 Maximal Rectangle](ch08-stacks/maximal-rectangle.md)
221+
- [8.22 Check If A Parentheses String Can Be Valid](ch08-stacks/check-if-a-parentheses-string-can-be-valid.md)
199222

200223
- [9. Strings](ch09-strings/index.md)
201224
- [9.0 Pattern Primer: The Three Lenses](ch09-strings/pattern-primer.md)
@@ -216,6 +239,12 @@
216239
- [9.15 Text Justification](ch09-strings/text-justification.md)
217240
- [9.16 Length Of Last Word](ch09-strings/length-of-last-word.md)
218241
- [9.17 Merge Strings Alternately](ch09-strings/merge-strings-alternately.md)
242+
- [9.18 Goat Latin](ch09-strings/goat-latin.md)
243+
- [9.19 Detect Capital](ch09-strings/detect-capital.md)
244+
- [9.20 Is Subsequence](ch09-strings/is-subsequence.md)
245+
- [9.21 String Compression](ch09-strings/string-compression.md)
246+
- [9.22 Custom Sort String](ch09-strings/custom-sort-string.md)
247+
- [9.23 Rank Teams By Votes](ch09-strings/rank-teams-by-votes.md)
219248

220249
- [10. Hash Tables & Sets](ch10-hash-tables/index.md)
221250
- [10.0 Pattern Primer: O(1) Lookup, Three Moves](ch10-hash-tables/pattern-primer.md)
@@ -241,6 +270,11 @@
241270
- [10.20 Unique Length-3 Palindromic Subsequences](ch10-hash-tables/unique-length-3-palindromic-subsequences.md)
242271
- [10.21 Max Number Of K-Sum Pairs](ch10-hash-tables/max-number-of-k-sum-pairs.md)
243272
- [10.22 Find Winner On A TicTacToe Game](ch10-hash-tables/find-winner-on-a-tic-tac-toe-game.md)
273+
- [10.23 Largest Time For Given Digits](ch10-hash-tables/largest-time-for-given-digits.md)
274+
- [10.24 Contiguous Array](ch10-hash-tables/contiguous-array.md)
275+
- [10.25 Set Mismatch](ch10-hash-tables/set-mismatch.md)
276+
- [10.26 Detect Squares](ch10-hash-tables/detect-squares.md)
277+
- [10.27 Snapshot Array](ch10-hash-tables/snapshot-array.md)
244278

245279
- [11. Greedy](ch11-greedy/index.md)
246280
- [11.0 Pattern Primer: The Local Choice, Defended](ch11-greedy/pattern-primer.md)
@@ -259,6 +293,16 @@
259293
- [11.13 Destroying Asteroids](ch11-greedy/destroying-asteroids.md)
260294
- [11.14 Employee Free Time](ch11-greedy/employee-free-time.md)
261295
- [11.15 Max Profit Assigning Work](ch11-greedy/max-profit-assigning-work.md)
296+
- [11.16 Best Time To Buy And Sell Stock](ch11-greedy/best-time-to-buy-and-sell-stock.md)
297+
- [11.17 Best Time To Buy And Sell Stock III](ch11-greedy/best-time-to-buy-and-sell-stock-iii.md)
298+
- [11.18 Best Time To Buy And Sell Stock With Cooldown](ch11-greedy/best-time-to-buy-and-sell-stock-with-cooldown.md)
299+
- [11.19 Best Time To Buy And Sell Stock With Transaction Fee](ch11-greedy/best-time-to-buy-and-sell-stock-with-transaction-fee.md)
300+
- [11.20 Find Minimum Ticket Price](ch11-greedy/find-minimum-ticket-price.md)
301+
- [11.21 Car Pooling](ch11-greedy/car-pooling.md)
302+
- [11.22 Meeting Scheduler](ch11-greedy/meeting-scheduler.md)
303+
- [11.23 Count Collisions On A Road](ch11-greedy/count-collisions-on-a-road.md)
304+
- [11.24 Partition Labels](ch11-greedy/partition-labels.md)
305+
- [11.25 Break A Palindrome](ch11-greedy/break-a-palindrome.md)
262306

263307
- [12. Backtracking](ch12-backtracking/index.md)
264308
- [12.0 Pattern Primer: DFS With an Undo Button](ch12-backtracking/pattern-primer.md)
@@ -344,6 +388,8 @@
344388
- [17.13 Minimum Genetic Mutations](ch17-advanced-graphs/minimum-genetic-mutations.md)
345389
- [17.14 Find Articulation Points](ch17-advanced-graphs/find-articulation-points.md)
346390
- [17.15 Longest Path With Different Adjacent Characters](ch17-advanced-graphs/longest-path-with-different-adjacent-characters.md)
391+
- [17.16 The Great Town Split](ch17-advanced-graphs/the-great-town-split.md)
392+
- [17.17 Floyd-Warshall](ch17-advanced-graphs/floyd-warshall.md)
347393

348394
- [18. Design & Caches](ch18-design-caches/index.md)
349395
- [18.0 Pattern Primer: Composing Structures](ch18-design-caches/pattern-primer.md)

‎CodingInterviewFightClub/src/ch02-dynamic-programming/index.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
| 2.25 | Cherry Pickup | two-walker DP | $O(n^3)$ | [→](cherry-pickup.md) |
3939
| 2.26 | Racecar | (pos, speed) DFS | $O(win·sp)$ | [→](racecar.md) |
4040
| 2.27 | Min Taps To Water Garden | interval covering greedy | $O(n)$ | [→](minimum-number-of-taps.md) |
41+
| 2.28 | Shortest Common Supersequence | LCS + backtrace | $O(mn)$ | [→](shortest-common-supersequence.md) |
42+
| 2.29 | Min Cost Climbing Stairs | two-step DP | $O(n)$ | [→](min-cost-climbing-stairs.md) |
4143
## Reading order
4244

4345
2.2 and 2.3 first — they're the *ur-examples* of the state-shape `dp[i][j]`. Then 2.1 (same table, different recurrence), then the knapsack family (2.4–2.6) which is the most frequently re-appearing pattern in real interviews, then the interval DPs (2.10, 2.11), then the gyms (2.8, 2.9, 2.13). End with 2.12 which is the *anti-DP* — it proves you know when **not** to reach for a DP table.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# 2.29 Min Cost Climbing Stairs
2+
3+
> **Source**: [`src/main/kotlin/array/dp/MinCostClimbingStaris.kt`](https://github.com/arpanpathak/AdvancedAlgorithmPatterns/blob/main/src/main/kotlin/array/dp/MinCostClimbingStaris.kt)
4+
> **Pattern**: two-step DP · **Core page**
5+
6+
## The Problem
7+
8+
Climb to the top paying `cost[i]` per step; take 1 or 2 steps. Min cost.
9+
10+
- Constraints: n ≥ 2.
11+
12+
## Examples
13+
14+
```
15+
Input: cost = [10,15,20] -> Output: 15 (start at 1, pay 15, jump past top)
16+
Input: cost = [1,100,1,1,1,100,1,1,100,1] -> Output: 6
17+
```
18+
19+
## Intuition — the cheapest way to stand on step i
20+
21+
`dp[i]` = min cost to reach step i = `cost[i] + min(dp[i-1], dp[i-2])` — the [2.4](house-robber.md) neighbor-choice DP:
22+
23+
```kotlin
24+
val dp = IntArray(cost.size)
25+
dp[0] = cost[0]
26+
dp[1] = cost[1]
27+
28+
for (i in 2 until cost.size) {
29+
dp[i] = cost[i] + minOf(dp[i - 1], dp[i - 2])
30+
}
31+
return minOf(dp[cost.size - 1], dp[cost.size - 2]) // finish from either top step
32+
```
33+
34+
**Why return the min of the last two?** The top is *past* the last step — you can finish from step n-1 or n-2 (one 2-step jump). The answer is the cheaper landing.
35+
36+
**Why the [2.4](house-robber.md) shape?** Each step's best depends only on the previous two — a linear DP with constant lookback.
37+
38+
## Approach 1 — Full DP array (the repo's version)
39+
40+
## Approach 2 — Two rolling variables (O(1) space)
41+
42+
`prev2, prev1` updated per step — same recurrence, no array.
43+
44+
```kotlin
45+
class MinCostClimbingStaris {
46+
/**
47+
* @param cost step costs
48+
* @return min cost to reach the top
49+
*/
50+
fun minCostClimbingStairs(cost: IntArray): Int {
51+
if (cost.size <= 2) return cost.min()
52+
53+
val dp = IntArray(cost.size)
54+
dp[0] = cost[0]
55+
dp[1] = cost[1]
56+
57+
for (i in 2 until cost.size) {
58+
dp[i] = cost[i] + minOf(dp[i - 1], dp[i - 2])
59+
}
60+
return minOf(dp[cost.size - 1], dp[cost.size - 2])
61+
}
62+
}
63+
```
64+
65+
```java
66+
public class MinCostClimbingStairs {
67+
/**
68+
* @param cost step costs
69+
* @return min cost to reach the top
70+
*/
71+
public int minCostClimbingStairs(int[] cost) {
72+
int prev2 = cost[0], prev1 = cost[1];
73+
74+
for (int i = 2; i < cost.length; i++) {
75+
int cur = cost[i] + Math.min(prev1, prev2);
76+
prev2 = prev1;
77+
prev1 = cur;
78+
}
79+
return Math.min(prev1, prev2);
80+
}
81+
}
82+
```
83+
84+
```cpp
85+
#include <vector>
86+
#include <algorithm>
87+
88+
class MinCostClimbingStairs {
89+
public:
90+
/**
91+
* @param cost step costs
92+
* @return min cost to reach the top
93+
*/
94+
int minCostClimbingStairs(std::vector<int>& cost) {
95+
int prev2 = cost[0], prev1 = cost[1];
96+
97+
for (int i = 2; i < (int)cost.size(); i++) {
98+
int cur = cost[i] + std::min(prev1, prev2);
99+
prev2 = prev1;
100+
prev1 = cur;
101+
}
102+
return std::min(prev1, prev2);
103+
}
104+
};
105+
```
106+
107+
```python
108+
def min_cost_climbing_stairs(cost: list[int]) -> int:
109+
"""
110+
@param cost: step costs
111+
@return: min cost to reach the top
112+
"""
113+
prev2, prev1 = cost[0], cost[1]
114+
115+
for c in cost[2:]:
116+
prev2, prev1 = prev1, c + min(prev1, prev2)
117+
118+
return min(prev1, prev2)
119+
```
120+
121+
```rust
122+
impl Solution {
123+
/// @param cost step costs
124+
/// @return min cost to reach the top
125+
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
126+
let mut prev2 = cost[0];
127+
let mut prev1 = cost[1];
128+
129+
for &c in cost.iter().skip(2) {
130+
let cur = c + prev1.min(prev2);
131+
prev2 = prev1;
132+
prev1 = cur;
133+
}
134+
prev1.min(prev2)
135+
}
136+
}
137+
```
138+
139+
## Dry run
140+
141+
**Input:** `cost = [10,15,20]`.
142+
143+
```
144+
dp[0]=10, dp[1]=15.
145+
i=2: dp[2] = 20 + min(15, 10) = 30.
146+
return min(30, 15) = 15 ✓ (start at 1, pay 15, 2-step to the top)
147+
```
148+
149+
## Complexity
150+
151+
**Time.** One pass:
152+
153+
$$
154+
T(n) = O(n)
155+
$$
156+
157+
**Space.** O(n) or O(1):
158+
159+
$$
160+
S(n) = O(1)
161+
$$
162+
163+
## Variants & follow-ups
164+
165+
- **Climbing Stairs** — the no-cost counting twin.
166+
- **House Robber** ([2.4](house-robber.md)) — the same two-step lookback DP.
167+
- **Interview follow-up:** "Why the min of the last two dp entries?" The top isn't a paid step — it's reached from either of the last two. Ending the recurrence one step early and taking the cheaper landing IS the finish rule.

0 commit comments

Comments
 (0)