A quarry is surveyed as a grid of R rows and C columns of integer elevations. A hauler starts at the top-left cell (0,0) and must reach the bottom-right cell (R-1, C-1), moving only up, down, left, or right to an adjacent cell.
The strain of a route is the maximum, over all consecutive steps of the route, of the absolute difference in elevation between the two cells of that step. Among all routes, find the minimum possible strain. (A route that never moves — when the grid is a single cell — has strain 0.)
Line 1: two integers R and C.
Each of the next R lines: C space-separated integers, the elevations of that row.
A single integer: the minimum possible strain of a route from the top-left to the bottom-right cell.
Example 1
Input
2 2 1 3 2 4
Expected
2
Explanation
Going right then down has steps |1-3|=2 and |3-4|=1 (strain 2); going down then right has |1-2|=1 and |2-4|=2 (strain 2). The minimum strain is 2.
Example 2
Input
1 1 5
Expected
0
Explanation
The start and end are the same single cell, so no step is ever taken and the strain is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →