00:00

Problem Statement

Input/Output Examples

Visual Explanation

Each row depends only on the previous row, so one array of length n, updated in place, is enough.

Approaches

The DP builds the table; the formula counts arrangements of moves directly.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here m and n are the grid dimensions. The DP fills m × n cells; the formula needs min(m, n) multiplications.

Interview Tips and Recommended Approach

  • Initialise the first row and column to 1. There is only one way to move along an edge.
  • Know the obstacle variant. Cells with obstacles get 0, which the DP handles naturally; the formula does not.
  • Explain the formula as choosing which of the m + n - 2 moves are downs.
  • Watch overflow. Use BigInteger for very large grids.

Recommended Approach: One-Row DP

Why: it is simple, works unchanged when cells are blocked, and uses only one row of memory.