Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 0062-unique-paths/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

BFSでマスを管理しながら左と上のマスの経路数を足し合わせた。

### step2

ChatGPTを参考に、DPで解く解き方に変更。

### step3

3回通すまで書き直し。
34 changes: 34 additions & 0 deletions 0062-unique-paths/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> ways(m, vector<int>(n, 0));
ways[0][0] = 1;

queue<pair<int, int>> grids;
grids.emplace(0, 0);

while (!grids.empty()) {
auto[row, column] = grids.front();
grids.pop();

if (!(row == 0 && column == 0) && ways[row][column] != 0) {
continue;
}

if (row - 1 >= 0) {
ways[row][column] += ways[row - 1][column];
}
if (column - 1 >= 0) {
ways[row][column] += ways[row][column - 1];
}

if (row + 1 < m) {
grids.emplace(row + 1, column);
}
if (column + 1 < n) {
grids.emplace(row, column + 1);
}
}
return ways[m - 1][n - 1];
}
};
14 changes: 14 additions & 0 deletions 0062-unique-paths/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> ways(m, vector<int>(n, 1));

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
ways[i][j] = ways[i - 1][j] + ways[i][j - 1];
}
}

return ways[m - 1][n - 1];
}
};
14 changes: 14 additions & 0 deletions 0062-unique-paths/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> ways(m, vector<int>(n, 1));

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
ways[i][j] = ways[i - 1][j] + ways[i][j - 1];
}
}

return ways[m - 1][n - 1];
}
};