Ctrl K

Climbing Left to Right

Medium
You are given an m × n matrix field of positive numbers. You may start at any cell of the first (leftmost) column. From cell (r, c) you can move to one of the cells in the next column: (r - 1, c + 1), (r, c + 1) or (r + 1, c + 1), but only if the new cell holds a strictly larger number than the current one. Return the largest number of moves you can make.
Example 1
field=[[1, 6, 2, 9], [4, 3, 7, 8], [2, 5, 1, 3]] → 3

Start at the 1 in the top-left corner: 1 → 6 → 7 → 9 (right, down-right, up-right), three moves to the right edge.

Example 2
field=[[5, 4], [6, 2]] → 0

Both numbers in the second column are smaller than anything in the first, so no move is possible.

Example 3
field=[[3, 4, 1], [9, 9, 9]] → 2
Constraints
  • 2 ≤ m, n ≤ 1000
  • 4 ≤ m · n ≤ 10⁵
  • 1 ≤ field[i][j] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWarehouse Cart Routes MediumCheapest Corner-to-Corner Route MediumLargest Solid Square MediumBest Spot for the Turret MediumSteps to the Nearest Zero Medium
loading Python…
Input
field=[[1, 6, 2, 9], [4, 3, 7, 8], [2, 5, 1, 3]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 84 tests on the server, hidden ones included.