Given an m x n integers matrix, return the length of the longest increasing path inmatrix.From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
from functools import cacheclass Solution: # Time: O(m * n) # Space: O(m * n) def longest_increasing_path(self, matrix: list[list[int]]) -> int: if not matrix or not matrix[0]: return 0 rows, cols = len(matrix), len(matrix[0]) directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] @cache def dfs(r: int, c: int) -> int: max_length = 1 for dr, dc in directions: nr, nc = r + dr, c + dc if 0 <= nr < rows and 0 <= nc < cols and matrix[nr][nc] > matrix[r][c]: max_length = max(max_length, 1 + dfs(nr, nc)) return max_length result = 0 for i in range(rows): for j in range(cols): result = max(result, dfs(i, j)) return result