일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 파비최
- Daily Challenge
- 운동
- 월간
- Problem Solving
- 프로젝트
- 만화도
- 개발자
- leetcode
- 매일
- English
- 3줄정리
- 리얼 클래스
- 뭐든
- 미드시청
- Writing
- realclass
- 영어공부
- 화상영어
- 잡생각
- 30분
- 10분
- 괜찮음
- 읽기
- FIT XR
- 링피트
- 스탭퍼
- 쓰릴오브파이트
- 사이드
- 영어원서읽기
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.06.18 Today's Challenge 본문
https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/
Number of Increasing Paths in a Grid - LeetCode
Can you solve this real interview question? Number of Increasing Paths in a Grid - You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions. Return the number of strictly increasing paths in the gr
leetcode.com
DFS + sort 로 푸는 문제
class Solution:
def countPaths(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
mod = 10 ** 9 + 7
directions = [[0,1], [0,-1], [1,0], [-1,0]]
dp = [[1] * n for _ in range(m)]
clist = [[i,j] for i in range(m) for j in range(n)]
clist.sort(key=lambda x: grid[x[0]][x[1]])
for i, j in clist:
for di, dj in directions:
ci, cj = i + di, j + dj
if 0 <= ci < m and 0 <= cj < n and grid[ci][cj] > grid[i][j]:
dp[ci][cj] += dp[i][j]
dp[ci][cj] %= mod
return sum(sum(row) % mod for row in dp) % mod
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.06.20 Today's Challenge (0) | 2023.06.20 |
---|---|
2023.06.19 Today's Challenge (0) | 2023.06.19 |
2023.06.17 Today's Challenge (0) | 2023.06.17 |
2023.06.16 Today's Challenge (0) | 2023.06.16 |
2023.06.15 Today's Challenge (0) | 2023.06.15 |
Comments