# 2D DP and DP on Grids

> Extend DP to two-dimensional states: grid paths, triangle and falling paths, and problems with an extra parameter such as a day or a choice. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/dp-2d-grids
Last updated: 2026-11-28

Extend DP to two-dimensional states: grid paths, triangle and falling paths, and problems with an extra parameter such as a day or a choice.

Intermediate · about 8 hours · difficulty 4/5

Learn first: [Dynamic Programming: Foundations and 1D DP](https://www.internhack.in/roadmaps/tracks/dsa-placement/dp-1d)

Many DP problems need two parameters in the state. Grids are the easiest place to build that skill.

### Grid DP

- **Unique Paths** and **Unique Paths II** (with obstacles): dp\[r\]\[c\] = dp\[r - 1\]\[c\] + dp\[r\]\[c - 1\].
- **Minimum Path Sum**: take the minimum instead of the sum.
- **Triangle**: start from the bottom row for a cleaner recurrence.
- **Minimum Falling Path Sum**: variable start and end column.
- **Cherry Pickup II**: two walkers moving together, state (row, col1, col2), a 3D DP.
- **Dungeon Game**: solve backwards from the princess.

### Index plus extra parameter

- Ninja's Training (GfG): state (day, last activity).
- Best Time to Buy and Sell Stock with cooldown, with fee, at most k transactions: state (index, holding or not, transactions left).
- Maximal Square: dp\[r\]\[c\] = 1 + min of three neighbours.

### Space optimisation

If row r depends only on row r - 1, keep two rows (or one, with care about overwrite order). Interviewers often ask this as a follow-up.

### Pitfalls

- Off-by-one when padding the table with an extra row and column.
- Iterating in an order where a cell is read before it is computed.
- Treating a backward problem (Dungeon Game) as forward and getting wrong minimums.

You are ready to move on when you can write Minimum Path Sum with O(cols) space and explain the state in the stock problems in words.

## Key concepts

- **Grid path counting**
- **Minimum path sum and triangle**
- **Variable start and end points**
- **3D DP with two walkers**
- **Stock buy and sell state machines**
- **Row-wise space optimisation**

## Mini project

Solve these 10 in 7 days, log time and pattern: Unique Paths, Unique Paths II, Minimum Path Sum, Triangle, Minimum Falling Path Sum, Maximal Square, Ninja's Training (GfG), Best Time to Buy and Sell Stock with Cooldown, Best Time to Buy and Sell Stock IV, Cherry Pickup II.

## Check yourself

For Best Time to Buy and Sell Stock IV, what is the state, and how many states are there?

## Free resources

- [takeUforward DP series](https://takeuforward.org/) (takeUforward)
- [AtCoder Educational DP Contest](https://atcoder.jp/contests/dp) (AtCoder)
- [LeetCode problem set](https://leetcode.com/problemset/) (LeetCode)

Previous: [Dynamic Programming: Foundations and 1D DP](https://www.internhack.in/roadmaps/tracks/dsa-placement/dp-1d) · Next: [Knapsack and Subset DP](https://www.internhack.in/roadmaps/tracks/dsa-placement/knapsack-dp)

## More in Greedy and Dynamic Programming

- [Greedy Algorithms](https://www.internhack.in/roadmaps/tracks/dsa-placement/greedy)
- [Dynamic Programming: Foundations and 1D DP](https://www.internhack.in/roadmaps/tracks/dsa-placement/dp-1d)
- [2D DP and DP on Grids](https://www.internhack.in/roadmaps/tracks/dsa-placement/dp-2d-grids)
- [Knapsack and Subset DP](https://www.internhack.in/roadmaps/tracks/dsa-placement/knapsack-dp)
- [DP on Subsequences and Strings: LIS, LCS, Edit Distance](https://www.internhack.in/roadmaps/tracks/dsa-placement/dp-sequences)
- [Interval DP, DP on Trees and Bitmask DP](https://www.internhack.in/roadmaps/tracks/dsa-placement/advanced-dp)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
