Ctrl K

Climbing Along Rows and Columns

Hard
You are given an m × n integer grid grid. You may start in any cell. In one move you can jump to **any** other cell in the same row or the same column (not necessarily adjacent), as long as its value is strictly greater than the current one. Keep moving for as long as you can. Return the largest number of cells a single route can visit, counting the starting cell.
Example 1
grid=[[3, 8, 4], [1, 6, 9]] → 4

For example 1 → 3 (up the column) → 4 (along the row) → 9 (down the column): four cells. Five is out of reach.

Example 2
grid=[[5, 5], [5, 5]] → 1

All values are equal and moves need a strictly larger value, so a route is a single cell.

Constraints
  • 1 ≤ m, n
  • m · n ≤ 10⁵
  • -10⁵ ≤ grid[i][j] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFlowers in Bloom on Visit Day HardWords Hidden in a Text MediumShuttle Driver Earnings MediumSquaring Ladder MediumSell Plots to Bidders Medium
loading Python…
Input
grid=[[3, 8, 4], [1, 6, 9]]
Expected
4

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