-
Notifications
You must be signed in to change notification settings - Fork 0
63. Unique Paths II #34
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,94 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { | ||
| vector<vector<int>> num_paths(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0)); | ||
| if (obstacleGrid[0][0] == 1) { | ||
| return 0; | ||
| } | ||
| num_paths[0][0] = 1; | ||
| for (int i = 0; i < obstacleGrid.size(); i++) { | ||
| for (int j = 0; j < obstacleGrid[0].size(); j++) { | ||
| if (obstacleGrid[i][j] == 1) { | ||
| continue; | ||
| } | ||
| if (i != 0) { | ||
| num_paths[i][j] += num_paths[i-1][j]; | ||
| } | ||
| if (j != 0) { | ||
| num_paths[i][j] += num_paths[i][j-1]; | ||
| } | ||
| } | ||
| } | ||
| return num_paths.back().back(); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 他の人の解答を見てみる | ||
|
|
||
| https://github.com/ryosuketc/leetcode_arai60/pull/47/files | ||
| https://github.com/tokuhirat/LeetCode/pull/34/files | ||
| https://github.com/irohafternoon/LeetCode/pull/37/files | ||
| https://github.com/philip82148/leetcode-swejp/pull/10/files | ||
|
|
||
| 最初の行と列を別の枠で初期化しているやり方をしているコードも結構あるが、個人的には`i != 0`や`i > 0`で条件分岐するやり方のほうが見やすいと感じた。 | ||
|
|
||
| 列のサイズの配列で行うバージョン | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { | ||
| int num_rows = obstacleGrid.size(); | ||
| int num_columns = obstacleGrid[0].size(); | ||
| vector<int> num_paths_current_row(num_columns, 0); | ||
| num_paths_current_row[0] = 1; | ||
| for (int i = 0; i < num_rows; i++) { | ||
| for (int j = 0; j < num_columns; j++) { | ||
| if (obstacleGrid[i][j] == 1) { | ||
| num_paths_current_row[j] = 0; | ||
| continue; | ||
| } | ||
| if (j > 0) { | ||
| num_paths_current_row[j] += num_paths_current_row[j - 1]; | ||
| } | ||
| } | ||
| } | ||
| return num_paths_current_row.back(); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 最終コード | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { | ||
| int num_rows = obstacleGrid.size(); | ||
| int num_columns = obstacleGrid[0].size(); | ||
| vector<vector<int>> num_paths(num_rows, vector<int>(num_columns, 0)); | ||
| if (obstacleGrid[0][0] == 1) { | ||
| return 0; | ||
| } | ||
|
Comment on lines
+74
to
+76
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. たしかにそうですね! |
||
| num_paths[0][0] = 1; | ||
| for (int i = 0; i < num_rows; i++) { | ||
|
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. このあたりのコメントを考慮したとき、私の感覚では今回は行列ではなくて座標かなと感じました。 加えて、row, col の方が num_rows, num_cols という命名とも対称性があってよいような気がします。
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. 確かにnum_rows, num_columnsとの対照性は気になりますね。ありがとうございます。 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.
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement
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. この仕様は知らなかったので助かります。ありがとうございます。 |
||
| for (int j = 0; j < num_columns; j++) { | ||
| if (obstacleGrid[i][j] == 1) { | ||
| continue; | ||
| } | ||
|
Comment on lines
+80
to
+82
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 (i != 0) { | ||
| num_paths[i][j] += num_paths[i-1][j]; | ||
| } | ||
| if (j != 0) { | ||
| num_paths[i][j] += num_paths[i][j-1]; | ||
| } | ||
|
Comment on lines
+83
to
+88
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 num_paths.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.
ご存じかもしれませんが、using namespace std; についてのコメントを共有いたします。
colorbox/leetcode#27 (comment)
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.
これは把握していなかったので助かります。ありがとうございます。