Skip to main content

Create Grid With Exactly K Paths I - Solution & Explanation

Practice this problem

Problem Statement

You are given three integers m, n, and k.

Construct any m x n grid consisting only of the characters '.' and '#', where:

  • '.' represents a free cell.
  • '#' represents an obstacle cell.

A valid path is a sequence of free cells that:

  • Starts at the top-left cell (0, 0).
  • Ends at the bottom-right cell (m - 1, n - 1).
  • Moves only:
    • Right, from (i, j) to (i, j + 1), or
    • Down, from (i, j) to (i + 1, j).

Return any grid such that there are exactly k valid paths from the top-left cell to the bottom-right cell. If no such grid exists, return an empty array.

 

Example 1:

Input: m = 2, n = 3, k = 2

Output: ["...","#.."]

Explanation:

There are exactly k = 2 valid paths from (0, 0) to (1, 2):

  • (0, 0) → (0, 1) → (0, 2) → (1, 2)
  • (0, 0) → (0, 1) → (1, 1) → (1, 2)

Example 2:

Input: m = 3, n = 3, k = 4

Output: ["..#","...","#.."]

Explanation:

There are exactly k = 4 valid paths from (0, 0) to (2, 2):

  • (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2)
  • (0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)
  • (0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)
  • (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2)

Example 3:

Input: m = 1, n = 4, k = 2

Output: []

Explanation:​

No grid exists with exactly k = 2 valid paths for a 1 x 4 grid, so the answer is an empty array.

 

Constraints:

  • 1 <= m, n <= 10
  • 1 <= k <= 4

Approach Overview

Problem Overview: You need to construct a grid where the number of valid paths from the start cell to the destination is exactly k. The challenge is not path traversal itself, but designing the grid so the path count matches the target value precisely.

Approach 1: Exhaustive Grid Construction (Exponential Time, Exponential Space)

The brute force strategy generates different grid layouts and computes the number of paths using DFS or dynamic programming after every modification. You iterate through combinations of blocked and open cells until the path count equals k. This approach demonstrates the underlying counting logic, but the search space grows exponentially as grid dimensions increase. It is mainly useful for validating small examples or debugging a more optimized construction strategy.

Approach 2: Dynamic Programming Path Counting (O(m * n) Time, O(m * n) Space)

Once a candidate grid is created, you can compute the number of valid paths using classic dynamic programming. Define dp[i][j] as the number of ways to reach cell (i, j). Each state accumulates values from the top and left neighbors while skipping blocked cells. This approach does not solve the construction problem alone, but it becomes the verification engine used by most accepted solutions. It is reliable when grid sizes are small enough to recompute path counts repeatedly.

Approach 3: Binary Construction with Controlled Paths (O(log k) Time, O(log k) Space)

The optimal solution treats the grid like a combinational structure where each branch contributes a known number of paths. You encode the value of k using carefully connected rows and columns so each split doubles or adds a fixed number of routes. The key insight is that path counts combine predictably, allowing you to represent large values using only a small grid. Most implementations build the structure incrementally using bit operations and verify the result with lightweight graph traversal or DP checks. This construction scales efficiently even when k is large.

Approach 4: DAG Interpretation of the Grid (O(V + E) Time, O(V) Space)

You can also model the grid as a directed acyclic graph where each open cell is a node and moves represent edges. Counting paths becomes a standard DAG DP problem processed in topological order. This perspective helps when reasoning about how adding or removing edges changes the total number of paths. It is especially useful in interviews because it connects the problem to reusable concepts from DP on graphs and path counting.

Recommended for interviews: Interviewers typically expect the constructive DP or binary-encoding approach because it shows you understand both path counting and controlled graph construction. Starting with brute force demonstrates intuition, but moving to logarithmic-size construction proves stronger algorithmic skill and complexity awareness.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Exhaustive Grid ConstructionExponentialExponentialSmall grids or debugging path logic
Dynamic Programming VerificationO(m * n)O(m * n)Counting paths in a fixed grid
Binary Constructive DesignO(log k)O(log k)General optimal solution for large k
DAG Path CountingO(V + E)O(V)Graph-based reasoning and interview discussions

Video Solution

LeetCode 3988 | Create Grid With Exactly K Paths I | Weekly Contest 510 Q3 | Construction Explained🔥 • CodeSprint • 776 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Create Grid With Exactly K Paths I easy or hard?
The problem is generally considered medium difficulty because the challenge is conceptual rather than implementation-heavy. Understanding how to control path counts through construction takes more insight than standard grid traversal problems.
Create Grid With Exactly K Paths I Python/Java solution
Python solutions usually use lists and recursive or iterative DP for counting paths after constructing the grid. Java implementations often use arrays for deterministic memory usage and iterative state transitions. Both languages support the same O(log k) constructive strategy.
How to solve Create Grid With Exactly K Paths I in O(log k)?
Use a grid construction where path counts combine additively through controlled branching. By mapping powers of two to specific grid patterns, you can represent k using only logarithmic layers. Dynamic programming is then used to confirm the exact number of paths.
What is the best approach for Create Grid With Exactly K Paths I?
The strongest approach uses constructive design combined with dynamic programming. The idea is to build a compact grid where each branch contributes a predictable number of paths, often based on the binary representation of k. This reduces the construction size while keeping verification efficient.
Is Create Grid With Exactly K Paths I asked at Google/Amazon/Meta?
Constructive graph and path-counting problems appear frequently in interviews at companies like Google and Meta because they test DP, graph modeling, and combinational reasoning together. Variants of exact-path construction are common in competitive programming rounds as well.
What data structure is used in Create Grid With Exactly K Paths I?
Most solutions rely on 2D grids, dynamic programming tables, and graph-style traversal logic. Some optimized implementations also use adjacency lists or bit manipulation to encode the contribution of each branch efficiently.
What is the time complexity of Create Grid With Exactly K Paths I?
The optimal constructive solution typically runs in O(log k) time and space because each added structure represents a bit or partial contribution to the total path count. A separate DP verification step on the generated grid runs in O(m * n).

Ready to solve this problem?

Practice Create Grid With Exactly K Paths I with our built-in code editor and test cases.

Practice on FleetCode