hard2-D Dynamic ProgrammingGoogleFacebook
Hillside Climber
A hiker finds the longest strictly ascending trail on an elevation grid moving one cell at a time (4-way).
Given an m×n matrix of integers, return the length of the longest strictly increasing path moving up/down/left/right.
Choose approach
9
cur
9
4
6
6
8
2
1
1
1/10
Narrationstep 1 of 10
Memoized longest from (0,0) = 1. Global best = 1.
Variables
r=0
c=0
local=1
best=1
How to think about it
- Strictly increasing -> no cycles -> clean memoization.
- Each cell computed exactly once.
Edge cases to catch in interview
- All equal -> 1.
- Single cell -> 1.
Examples
Example 1
4{"matrix":[[3,4,5],[3,2,6],[2,2,1]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed