MEDIUM NC#111 Blind #25 Dynamic Programming 2D (can be 1D optimized)

62. Unique Paths

📖 Problem

A robot is located at the top-left corner of a m x n grid. The robot can only move either down or right. How many possible unique paths to reach bottom-right corner?

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • Array DP table updates
  • State transition thinking
  • Base case initialization

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply Dynamic Programming reasoning pattern
  • Apply Recursion reasoning pattern

💡 Approach

  • dp[i][j] = paths to reach cell (i, j)
  • dp[i][j] = dp[i-1][j] + dp[i][j-1]
  • First row and first column initialized to 1
  • Time: O(m * n), Space: O(m * n) or O(n) optimized
  • 2, m-1), Recursion

🛠️ Hints & Pitfalls

Hints

  • dp[i][j] = paths to reach cell (i, j)
  • dp[i][j] = dp[i-1][j] + dp[i][j-1]
  • First row and first column initialized to 1

Common Pitfalls

  • Time: O(m * n), Space: O(m * n) or O(n) optimized
  • 2, m-1), Recursion

🧪 Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
uniquePaths(3, 7);
Expected:
28
Test Case 2
Not run
Input:
uniquePaths(3, 2);
Expected:
3
Test Case 3
Not run
Input:
uniquePaths(1, 1);
Expected:
1

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit