Here is the problem...
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 <= 1000 * 1 <= k <= 1e12








here is the same problem with smaller constraints:
https://leetcode.com/problems/create-grid-with-exactly-k-paths-i/description/
105259B - Mazes has the same setup, but it lets you choose $$$n$$$ and $$$m$$$.As for your problem, when $$$n \gt 80$$$ and $$$m \gt 40$$$, you can just reuse a construction from my problem to get a valid answer, but I'm not sure whether there is a general solution because you may have constraints under which most simple constructions would fail but there would still exist complex ones which only work for some values of $$$k$$$.
I'm away so if I solve it'll be in a reply/edit but the main thing I found that is probably a path to the solution. First it is far easier to assume all obstacles then build a path.
So realise the path going to bottomleftmost node (i.e.full bottom then full right) node is 1 path exactly.
if we go to the above row and reveal one column we get 1 more apth. if we add another we get 1, and so on. so thats clean.
So the pattern, om each row: then if we add another column, we get 1 x number of paths on the bottom row following this node, meaning it gets complicated but its workable.
i.e 1st node we add on row 3 is the number of revealed row 2 columns — 1. then 2nd column we reveal is number of revealed row2 cols -1, and so on. because we now have got a new way to reach every one of those nodes following this one.
the same applies continuously i.e row 4, first coll we add is the sum of row 3 contrib, nezt col is sum of row 3 contrib — first, and so on and so forth.
So you can dp this, here you can pull DP (number of paths = number of paths in the node to the right of it + number of paths in the node to the bottom of it) or the opposite way (number of total paths subtracted by the paths to the left with it, using a prefix sum). not fully sure. when i'm done ill do it
okay... I think I got the gist of the idea, but I'd be really grateful if you could solve it and provide the code here... It would help me understand the approach much better, & Thank you:)
I'm not 100% sure but I think I'm pretty close! This is a permutation problem. To see the number of possible path, you need to use this formula:
P = (m+n-2)!/((n-1)!*(m-1)!)
if P>K: you can solve it
if P=K: the solution is the given grid without obstacles
if P<K: there is no grid -> []
Now we need to look at the first case (P>K) (this is the part I'm not fully sure, but it should work)
Let's say we have m=5,n=4,k=6 and create the grid: . . . . . . . . . . . . . . . . . . . .
we can find the number of steps to reach any point by adding the number of steps of the points over ad on the left of the point we are considering. 1--1--1--1 1--2--3--4 1--3--6--10 1--4--10--20 1--5--15--35
if we use the formula from above P=35
since K=6 we need to find this grid: 1--?--?--? ?--?--?--? ?--?--?--? ?--?--?--? ?--?--?--6
I tried some approaches but i couldn't find one that would work on every possible set of input. I know this isn't the solution but at least you can eliminate 2 cases
Hi, if you're stil stuck here's my solution for this problem considering the constraints
can you explain me your approach? Thank you!
Top___Cat, now we cant make it a problem in our round :(