728x90
반응형
Level은 Medium으로 책정되었지만 좀 더 쉬운 난이도의 문제이다.
데이터 구조 Queue를 사용하고 Dynamic Programming 개념을 차용하면 풀 수 있다.
class Solution {
public:
int uniquePaths(int m, int n) {
int arr[100][100] = { 0 };
int AlreadyQueue[100][100] = { 0 };
queue<pair<int, int>> grid;
pair<int, int> SearchGrid;
grid.push(make_pair(0, 0));
arr[0][0] = 1;
while (!grid.empty())
{
SearchGrid = grid.front();
grid.pop();
// Go Down
if (SearchGrid.first < (m - 1)){
arr[SearchGrid.first + 1][SearchGrid.second] += arr[SearchGrid.first][SearchGrid.second];
if (AlreadyQueue[SearchGrid.first + 1][SearchGrid.second] == 0){
grid.push(make_pair(SearchGrid.first + 1, SearchGrid.second));
AlreadyQueue[SearchGrid.first + 1][SearchGrid.second] = 1;
}
else{
// Do Nothing
}
}
else{
// Do Nothing
}
// Go Right
if (SearchGrid.second < (n - 1)){
arr[SearchGrid.first][SearchGrid.second + 1] += arr[SearchGrid.first][SearchGrid.second];
if (AlreadyQueue[SearchGrid.first][SearchGrid.second + 1] == 0){
grid.push(make_pair(SearchGrid.first, SearchGrid.second + 1));
AlreadyQueue[SearchGrid.first][SearchGrid.second + 1] = 1;
}
else{
// Do Nothing
}
}
else{
// Do Nothing
}
}
return arr[m - 1][n - 1];
}
};
728x90
반응형
'코딩 문제' 카테고리의 다른 글
LeetCode 1768 Merge String Alternately (0) | 2023.07.17 |
---|---|
[LeetCode] 771, Jewels and Stones (0) | 2023.02.14 |
[LeetCode] 1720 Decode XORred Array (0) | 2023.01.29 |
[LeetCode] 728. Self Dividing Numbers, C++ (0) | 2023.01.26 |
[LeetCode] 2032. Two Out of Three, C++ (0) | 2023.01.25 |