일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 시바견
- Generator
- Convert Sorted List to Binary Search Tree
- iterator
- Decorator
- Substring with Concatenation of All Words
- LeetCode
- shiba
- Protocol
- Python Code
- t1
- Python
- concurrency
- 715. Range Module
- 프로그래머스
- Python Implementation
- 파이썬
- data science
- attribute
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- kaggle
- 운영체제
- Class
- 컴퓨터의 구조
- 43. Multiply Strings
- 밴픽
- DWG
Archives
- Today
- Total
Scribbling
[Programmers] 등굣길 본문
https://school.programmers.co.kr/learn/courses/30/lessons/42898
1. Python
def solution(m, n, puddles):
if m == 1 or n == 1:
return 0 if puddles else 1
puddles = set([(y-1, x-1) for y, x in puddles])
dp = [[0] * n for _ in range(m)]
dp[0][0] = 1
for x in range(1, n):
if (0, x) in puddles:
continue
dp[0][x] = dp[0][x-1]
for y in range(1, m):
if (y, 0) in puddles:
continue
dp[y][0] = dp[y-1][0]
for y in range(1, m):
for x in range(1, n):
if (y, x) in puddles:
continue
dp[y][x] = dp[y-1][x] + dp[y][x-1]
return dp[-1][-1] % 1000000007
2. C++
int solution(int m, int n, vector<vector<int>> puddles) {
if (m == 1 or n == 1)
return puddles.size() == 0 ? 1 : 0;
set<pair<int, int>> puddlesSet;
for (auto puddle : puddles) {
puddlesSet.insert(make_pair(puddle[0]-1, puddle[1]-1));
}
int dp[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
for (int x=1; x<n; x++) {
if (puddlesSet.find(make_pair(0, x)) != puddlesSet.end()) continue;
dp[0][x] = dp[0][x-1];
}
for (int y=1; y<m; y++) {
if (puddlesSet.find(make_pair(y, 0)) != puddlesSet.end()) continue;
dp[y][0] = dp[y-1][0];
}
for (int y=1; y<m; y++) {
for (int x=1; x<n; x++) {
if (puddlesSet.find(make_pair(y, x)) != puddlesSet.end()) continue;
dp[y][x] = dp[y-1][x] % 1000000007 + dp[y][x-1] % 1000000007;
}
}
return dp[m-1][n-1] % 1000000007;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 도둑질 (0) | 2024.06.18 |
---|---|
[Programmers] 사칙연산 (0) | 2024.06.18 |
[Programmers] 정수 삼각형 (0) | 2024.06.17 |
[Programmers] N으로 표현 (1) | 2024.06.13 |
Design HashMap -- Python Implementation (0) | 2023.10.05 |