|
| 1 | +from typing import List |
| 2 | +from pprint import pprint |
| 3 | + |
| 4 | + |
| 5 | +class Solution: |
| 6 | + def longestIncreasingPath(self, matrix: List[List[int]]) -> int: |
| 7 | + base_len = len(matrix) |
| 8 | + col_len = len(matrix[0]) |
| 9 | + range_base = range(base_len) |
| 10 | + range_col = range(col_len) |
| 11 | + longest_paths = [[1] * col_len for x in range_base] |
| 12 | + for i in range(len(matrix)): |
| 13 | + for j in range(len(matrix[0])): |
| 14 | + self.find_longest_path(matrix, longest_paths, i, j) |
| 15 | + max_len = 1 |
| 16 | + pprint(longest_paths) |
| 17 | + for each_row in longest_paths: |
| 18 | + max_len = max(max_len, max(each_row)) |
| 19 | + return max_len |
| 20 | + |
| 21 | + def find_longest_path( |
| 22 | + self, |
| 23 | + matrix: List[List[int]], |
| 24 | + dfs_table: List[List[int]], |
| 25 | + row: int, |
| 26 | + col: int, |
| 27 | + current_length: int = 1, |
| 28 | + ) -> None: |
| 29 | + if row != -1 and col != -1 and row < len(matrix) and col < len(matrix[0]): |
| 30 | + current_element = matrix[row][col] |
| 31 | + left_element = matrix[row][col - 1] if col > 0 else 1 |
| 32 | + right_element = matrix[row][col + 1] if col < len(matrix[0]) - 1 else 1 |
| 33 | + bottom_element = matrix[row + 1][col] if row < len(matrix) - 1 else 1 |
| 34 | + top_element = matrix[row - 1][col] if row > 0 else 1 |
| 35 | + |
| 36 | + if dfs_table[row][col] != 1: |
| 37 | + return |
| 38 | + |
| 39 | + if top_element > current_element: |
| 40 | + self.find_longest_path( |
| 41 | + matrix, dfs_table, row - 1, col, current_length + 1 |
| 42 | + ) # top |
| 43 | + if bottom_element > current_element: |
| 44 | + self.find_longest_path( |
| 45 | + matrix, dfs_table, row + 1, col, current_length + 1 |
| 46 | + ) |
| 47 | + if right_element > current_element: |
| 48 | + self.find_longest_path( |
| 49 | + matrix, dfs_table, row, col + 1, current_length + 1 |
| 50 | + ) |
| 51 | + if left_element > current_element: |
| 52 | + self.find_longest_path( |
| 53 | + matrix, dfs_table, row, col - 1, current_length + 1 |
| 54 | + ) |
| 55 | + |
| 56 | + dfs_left = ( |
| 57 | + dfs_table[row][col - 1] |
| 58 | + if col > 0 and matrix[row][col - 1] > matrix[row][col] |
| 59 | + else 0 |
| 60 | + ) |
| 61 | + dfs_right = ( |
| 62 | + dfs_table[row][col + 1] |
| 63 | + if col < len(matrix[0]) - 1 and matrix[row][col + 1] > matrix[row][col] |
| 64 | + else 0 |
| 65 | + ) |
| 66 | + dfs_bottom = ( |
| 67 | + dfs_table[row + 1][col] |
| 68 | + if row < len(matrix) - 1 and matrix[row + 1][col] > matrix[row][col] |
| 69 | + else 0 |
| 70 | + ) |
| 71 | + dfs_top = ( |
| 72 | + dfs_table[row - 1][col] |
| 73 | + if row > 0 and matrix[row - 1][col] > matrix[row][col] |
| 74 | + else 0 |
| 75 | + ) |
| 76 | + dfs_table[row][col] = ( |
| 77 | + max(dfs_left, max(dfs_right, max(dfs_bottom, dfs_top))) + 1 |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + sol = Solution() |
| 83 | + a = [[9, 9, 4], [6, 6, 8], [2, 1, 1]] |
| 84 | + print(sol.longestIncreasingPath(a)) |
0 commit comments