Pascal's Triangle II asks you to return only the k-th row (0-indexed) of Pascal's triangle using O(k) space. Unlike generating all rows, here you only need the final row. This easy coding problem tests space-optimized row computation. The array and dynamic programming interview pattern is demonstrated with a space optimization.
Goldman Sachs and Yahoo ask this to test space optimization of a DP problem. The naive approach builds all k rows, but with careful in-place updating, you can compute the k-th row using only O(k) space.
In-place reverse update. Initialize row = [1]. For each step i from 1 to k: update the row in reverse (right to left). For j from i down to 1: row[j] += row[j-1]. This mimics the Pascal's rule in-place without needing a previous row copy.
k=4. Start: [1]. Step 1: add 1 → [1,1]. Step 2: [1,2,1]. Step 3: [1,3,3,1]. Step 4: reverse update → [1,4,6,4,1]. Result: [1,4,6,4,1].
Alternatively: directly compute using C(k,0), C(k,1), ..., C(k,k) = 1, k, k(k-1)/2, ...
Pascal's Triangle II is the classic space-optimized DP problem. The reverse-update trick — updating right-to-left to avoid using old values — appears in many 0/1 knapsack optimizations. Practice this trick until it feels natural. Also know the direct formula approach using C(k,j) = C(k,j-1) * (k-j+1) / j, which computes each element in O(1) after the previous one.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Pascal's Triangle II | Easy | Solve | |
| Pascal's Triangle | Easy | Solve | |
| Pascal's Triangle | Easy | Solve | |
| Best Time to Buy and Sell Stock | Easy | Solve | |
| Min Cost Climbing Stairs | Easy | Solve |