[LeetCode][Python3] 329. Longest Increasing Path in a Matrix
2019. 3. 27. 00:25 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
My Solution :
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
if not matrix or not matrix[0]:
return 0
def find(y, x):
if not memo[y][x]:
val = matrix[y][x]
res = 1
for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1)):
if (0 <= y+dy < Y and 0 <= x+dx < X and
matrix[y+dy][x+dx] > val):
res = max(res, 1+find(y+dy, x+dx))
memo[y][x] = res
return memo[y][x]
Y, X = len(matrix), len(matrix[0])
memo = [[0]*X for _ in range(Y)]
res = 1
for y in range(Y):
for x in range(X):
res = max(res, find(y, x))
return res
Comment :
전형적인 DFS + Memoization 문제인데, 연습이 부족해서 그런지 매번 쉽게 풀리지 않는다. 비슷한 유형을 수십번 풀어보면 손에 익으려나...
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 239. Sliding Window Maximum (0) | 2019.03.29 |
---|---|
[LeetCode][Python3] 315. Count of Smaller Numbers After Self (0) | 2019.03.28 |
[LeetCode][Python3] 395. Longest Substring with At Least K Repeating Characters (0) | 2019.03.28 |
[LeetCode][Python3] 73. Set Matrix Zeroes (0) | 2019.03.27 |
[LeetCode][Python3] 264. Ugly Number II (0) | 2019.03.26 |
[LeetCode][Python3] 334. Increasing Triplet Subsequence (0) | 2019.03.25 |
[LeetCode][Python3] 297. Serialize and Deserialize Binary Tree (0) | 2019.03.25 |
[LeetCode][Python3] 105. Construct Binary Tree from Preorder and Inorder Traversal (0) | 2019.03.24 |
최근에 달린 댓글 최근에 달린 댓글