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
38 changes: 38 additions & 0 deletions 63/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//Time: 15:26
// Space Complexity: O(m*n)
// Time Complexity: O(m*n)

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int height = obstacleGrid.size();
int width = obstacleGrid.front().size();
vector<vector<int>> step_count(height, vector<int>(width, 0));

if (obstacleGrid[0][0] == 1) {
return 0;
}

step_count[0][0]=1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 代入演算子の前後にはスペースを置くスタイルが一般的に思います.cf

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます、このあたりは気をつけたいですね


for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; ++col) {
if (obstacleGrid[row][col] == 1) {
continue;
}
if (row == 0 && col == 0) {
continue;
}
int current_step_count = 0;
if (row > 0) {
current_step_count += step_count[row-1][col];
}
if (col > 0) {
current_step_count += step_count[row][col-1];
}
step_count[row][col] = current_step_count;
}
}
return step_count[height-1][width-1];
}
};
27 changes: 27 additions & 0 deletions 63/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid[0][0] == 1) {
return 0;
}
vector<vector<int>> pathCount(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0));
pathCount[0][0] = 1;
for (int i = 0; i < pathCount.size(); ++i) {
for (int j = 0; j < pathCount[0].size(); ++j) {
if (i == 0 && j == 0) {
continue;
}
Comment on lines +11 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

後半の処理と内容が重複しているので、消しても動きそうです。

Suggested change
if (i == 0 && j == 0) {
continue;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは確かにそうですね 🙏

if (obstacleGrid[i][j] == 1) {
continue;
}
if (i > 0) {
pathCount[i][j] += pathCount[i - 1][j];
}
if (j > 0) {
pathCount[i][j] += pathCount[i][j - 1];
}
}
}
return pathCount.back().back();
}
};
25 changes: 25 additions & 0 deletions 63/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid[0][0] == 1) { return 0; }
if (obstacleGrid.back().back() == 1) { return 0; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはなくても動きそうですね。高速化と可読性の兼ね合いですかね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

早期returnと可読性を取ってこのようにしました。


vector<vector<int>> pathCounter(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0));

pathCounter[0][0] = 1;
for (int y = 0; y < obstacleGrid.size(); ++y) {
for (int x = 0; x < obstacleGrid[0].size(); ++x) {
Comment on lines +10 to +11
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ただの好みですが,x,yよりrow, colあるいはi, jの方が自然な順番になって好きです.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このあたりは結構好みが分かれますが、問題によってcol,row/x,yで使い分けています

Comment on lines +10 to +11
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obstacleGrid.size()obstacleGrid[0].size() を一度変数に置いたほうが、考えている座標系が伝わりやすいと思いました。個人的には row, col が分かりやすいです(y, x はそれぞれの次元との対応が必ずしも決まっているとは限らないので)。

if (obstacleGrid[y][x] == 1) {
continue;
}
if (x > 0) {
pathCounter[y][x] += pathCounter[y][x - 1];
}
if (y > 0) {
pathCounter[y][x] += pathCounter[y - 1][x];
}
}
}
return pathCounter.back().back();
}
};