fork(1) download
  1. import operator
  2. class Solution(object):
  3. def longestIncreasingPath(self, matrix):
  4. """
  5. :type matrix: List[List[int]]
  6. :rtype: int
  7. """
  8. def check_boundary(matrix, (row, col)):
  9. return 0 <= row < len(matrix) and 0 <= col < len(matrix[0])
  10.  
  11. def dfs(current=(0,0), matrix, visited=set(), memory={}):
  12. cost = 0
  13. print(current)
  14. if not check_boundary(matrix, current):
  15. return -1
  16. if current in visited:
  17. return memory[current]
  18. for direction in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
  19. new = current[0] + direction[0], current[1] + direction[1]
  20. visited.add(current)
  21. print(new)
  22. if check_boundary(matrix, new) and matrix[current[0]][current[1]] > matrix[new[0]][new[1]]:
  23. cost = max(cost, 1 + dfs(new, matrix, visited, memory))
  24. memory[(current)] = cost
  25. return cost
  26.  
  27. if not matrix:
  28. return 0
  29. maximum = 0
  30. for i in range(len(matrix)):
  31. for j in range(len(matrix[0])):
  32. maximum = max(maximum, dfs((i,j), matrix))
  33. return maximum + 1
Runtime error #stdin #stdout #stderr 0.02s 64400KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
  File "prog.py", line 11
SyntaxError: non-default argument follows default argument