일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬
- Regular Expression
- Substring with Concatenation of All Words
- Python Implementation
- data science
- 프로그래머스
- Python Code
- 시바견
- 밴픽
- iterator
- shiba
- LeetCode
- Protocol
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 운영체제
- Decorator
- kaggle
- t1
- Generator
- concurrency
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- Python
- attribute
- 30. Substring with Concatenation of All Words
- 715. Range Module
- 109. Convert Sorted List to Binary Search Tree
- DWG
- Class
Archives
- Today
- Total
Scribbling
LeetCode: 329. Longest Increasing Path in a Matrix 본문
Computer Science/Coding Test
LeetCode: 329. Longest Increasing Path in a Matrix
focalpoint 2022. 1. 11. 17:26Tip of the day: A problem starts with "Longest" usually has something to do with DP.
Though the difficulty is hard, it's not a difficult one.
Seeing the below code will be enough for sure, so I won't give any explanation about it.
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
m, n = len(matrix), len(matrix[0])
self.matrix = matrix
self.visited = [[False] * n for _ in range(m)]
self.dp = [[-1] * n for _ in range(m)]
ret = 0
for y in range(m):
for x in range(n):
if not self.visited[y][x]:
ret = max(ret, self.dfs(y, x))
return ret
def dfs(self, y, x):
if self.visited[y][x]:
return self.dp[y][x]
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
self.visited[y][x] = True
self.dp[y][x] = 1
for d in range(4):
next_y, next_x = y + dy[d], x + dx[d]
if 0 <= next_y < len(self.matrix) and 0 <= next_x < len(self.matrix[0]):
if self.matrix[next_y][next_x] > self.matrix[y][x]:
self.dp[y][x] = max(self.dp[y][x], self.dfs(next_y, next_x) + 1)
return self.dp[y][x]
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 341. Flatten Nested List Iterator (0) | 2022.01.13 |
---|---|
LeetCode: 334. Increasing Triplet Subsequence (0) | 2022.01.11 |
LeetCode: 350. Intersection of Two Arrays II (0) | 2022.01.10 |
LeetCode: 322. Coin Change (0) | 2022.01.07 |
LeetCode: 21. Merge Two Sorted Lists (0) | 2022.01.05 |