-
Notifications
You must be signed in to change notification settings - Fork 0
63. Unique Paths II #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| 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]; | ||
| } | ||
| }; | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 後半の処理と内容が重複しているので、消しても動きそうです。
Suggested change
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||||
| } | ||||||||
| }; | ||||||||
| 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; } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これはなくても動きそうですね。高速化と可読性の兼ね合いですかね。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ただの好みですが,
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このあたりは結構好みが分かれますが、問題によってcol,row/x,yで使い分けています
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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(); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: 代入演算子の前後にはスペースを置くスタイルが一般的に思います.cf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントありがとうございます、このあたりは気をつけたいですね