| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 링피트
- 10분
- 화상영어
- 영어공부
- 월간
- 스탭퍼
- FIT XR
- 영어원서읽기
- 괜찮음
- 30분
- 3줄정리
- 리얼 클래스
- 프로젝트
- 쓰릴오브파이트
- Problem Solving
- 개발자
- Daily Challenge
- English
- 잡생각
- leetcode
- 파비최
- 뭐든
- 사이드
- 만화도
- 미드시청
- 매일
- Writing
- 읽기
- realclass
- 운동
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
Longest Increasing Path in a Matrix - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
음.. DP는 언제나 어려운 듯. 기본 문제인듯 ㅋㅋㅋ
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
m, n = len(matrix), len(matrix[0])
dp = [[-1] * n for _ in range(m)]
def dfs(i, j, prev):
if i < 0 or j < 0 or i >= m or j >=n or matrix[i][j] <= prev:
return 0
if dp[i][j] != -1:
return dp[i][j]
left = dfs(i, j-1, matrix[i][j])
right = dfs(i, j+1, matrix[i][j])
top = dfs(i-1, j, matrix[i][j])
bottom = dfs(i+1, j, matrix[i][j])
dp[i][j] = max(left, right, top, bottom) + 1
return dp[i][j]
ans = -1
for i in range(m):
for j in range(n):
ans = max(ans, dfs(i, j, -1))
return ans반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| Today's Challenge (0) | 2022.05.21 |
|---|---|
| Today's Challenge (0) | 2022.05.20 |
| Today's Challenge (0) | 2022.05.18 |
| Today's Challenge (0) | 2022.05.17 |
| Today's Challenge (0) | 2022.05.16 |
Comments