Skip to content
BytePatterns

Longest Climbing Path

HardMatrix & Grid#grid-dfs#memoization~40m

Problem

A grid holds whole numbers. A climbing path moves one step at a time up, down, left or right, and every step must land on a strictly larger value than the cell it leaves. Return the number of cells in the longest climbing path anywhere in the grid. The grid has at least one cell.

Examples

Input:  grid = [[9, 9, 4],
                [6, 6, 8],
                [2, 1, 1]]
Output: 4
Why:    1, 2, 6, 9 climbs up the left side
Input:  grid = [[3, 4, 5],
                [3, 2, 6],
                [2, 2, 1]]
Output: 4
Why:    3, 4, 5, 6 runs along the top and down the right
Input:  grid = [[7, 7],
                [7, 7]]
Output: 1
Why:    edge case, equal values never climb, so every path is a single cell

Hints

0 / 3

Stuck on the idea rather than the code? Grid Traversal covers it.