Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 4, 2025

Comment on lines +74 to +76
if (obstacleGrid[0][0] == 1) {
return 0;
}

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できるのではないかと思いました.

Copy link
Owner Author

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する感覚で書いていたので見落としておりました。

Comment on lines +80 to +82
if (obstacleGrid[i][j] == 1) {
continue;
}

Choose a reason for hiding this comment

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

== 1はマジックナンバーになっているので,defineしてあげたらわかりやすいと思います

Comment on lines +83 to +88
if (i != 0) {
num_paths[i][j] += num_paths[i-1][j];
}
if (j != 0) {
num_paths[i][j] += num_paths[i][j-1];
}

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));

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)

Copy link
Owner Author

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++) {

Choose a reason for hiding this comment

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

h1rosaka/arai60#22 (comment)

このあたりのコメントを考慮したとき、私の感覚では今回は行列ではなくて座標かなと感じました。

加えて、row, col の方が num_rows, num_cols という命名とも対称性があってよいような気がします。

Copy link
Owner Author

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++) {

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

この仕様は知らなかったので助かります。ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants