File size: 861 Bytes
d3f4f72 |
1 2 3 4 5 6 |
Any path from one corner of the grid to the opposite corner must include at least \(K = N + M - 1\) cells and therefore have a minimum duration of \(K\). Therefore, if \(A < K\) or \(B < K\), no valid grid exists.
Otherwise, consider a grid with a wait time of \(A - (N + M - 2)\) for the top-left cell, a wait time of \(B - (N + M - 2)\) for the top-right cell, and wait times of \(1\) for all remaining cells. In such a grid, the minimum duration of a path from the top-left to the bottom-right corner includes the top-left cell along with \(K - 1\) cells with wait times of \(1\), for a total of \(A - (N + M - 2) + (N + M - 2) = A\). The minimum duration of a path from the top-right to the bottom-left corner is similarly \(B\). Therefore, such a grid constitutes a valid output.
[See David Harmeyer's solution video here](https://youtu.be/R4ooAlRc93Y).
|