-
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?
Conversation
| if (obstacleGrid[0][0] == 1) { | ||
| return 0; | ||
| } |
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.
obstacleGrid[num_rows - 1][num_columns - 1]の時もpathは0になるので,早期returnできるのではないかと思いました.
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.
たしかにそうですね!
num_paths[0][0] = 1だとうまくいかないケースを場合分けするついでに早期returnする感覚で書いていたので見落としておりました。
| if (obstacleGrid[i][j] == 1) { | ||
| continue; | ||
| } |
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.
== 1はマジックナンバーになっているので,defineしてあげたらわかりやすいと思います
| if (i != 0) { | ||
| num_paths[i][j] += num_paths[i-1][j]; | ||
| } | ||
| if (j != 0) { | ||
| num_paths[i][j] += num_paths[i][j-1]; | ||
| } |
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.
処理がシンプルにまとまっていて,とても見やすいと思いました
参考にさせていただきます
| class Solution { | ||
| public: | ||
| int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { | ||
| vector<vector<int>> num_paths(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0)); |
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.
これは把握していなかったので助かります。ありがとうございます。
| return 0; | ||
| } | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
このあたりのコメントを考慮したとき、私の感覚では今回は行列ではなくて座標かなと感じました。
加えて、row, col の方が num_rows, num_cols という命名とも対称性があってよいような気がします。
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.
確かにnum_rows, num_columnsとの対照性は気になりますね。ありがとうございます。
| return 0; | ||
| } | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
++i のほうがよいかなと思いました。
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement
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.
この仕様は知らなかったので助かります。ありがとうございます。
問題文:
https://leetcode.com/problems/unique-paths-ii/description/