Given an m x n integer matrix, you may start from any cell and move up, down, left, or right. You may only move to a cell with a strictly larger value. Return the length of the longest valid path.
Input / output
matrix: int[][]Examples
matrix = [[9,9,4],[6,6,8],[2,1,1]] returns 4 from the path 1 -> 2 -> 6 -> 9.matrix = [[3,4,5],[3,2,6],[2,2,1]] returns 4 from the path 3 -> 4 -> 5 -> 6.matrix = [[1]] returns 1.Constraints
1 <= m, n <= 200-2^31 <= matrix[r][c] <= 2^31 - 1Target complexity
O(m * n) or O(m * n + edges) time, not exponential search.Hints
0. Repeatedly peel one topological layer at a time, or memoize DFS results per cell.Follow-up Why does allowing diagonal moves change only the neighbor count, not the core DAG reasoning?