Grid DP

Navigate 2D grids with DP for unique paths, minimum path sum, and dungeon problems.

0 Problems2-3 hours

Overview

Grid DP uses a 2D dp table where dp[i][j] represents the answer for the cell at row i, column j. The recurrence typically combines dp[i-1][j] (from above) and dp[i][j-1] (from left).

Complexity Summary

Time Complexity

O(m × n)

Space Complexity

O(m × n) reducible to O(n)

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

Unique paths

Concept

dp[i][j] = dp[i-1][j] + dp[i][j-1]

Practice questions for this pattern
2

Min path sum

Concept

dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])

Practice questions for this pattern
3

Obstacles

Concept

set dp[i][j] = 0 or Infinity if cell is blocked

Practice questions for this pattern
4

Space optimization

Concept

use a 1D rolling array instead of full 2D table

Practice questions for this pattern
5

Direction variants

Concept

sometimes right/down/left/up are all valid

Practice questions for this pattern
6

Dungeon / cherry pickup

Concept

harder variants requiring max/min tracking

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview