Skip to content
Open
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
94 changes: 94 additions & 0 deletions 63/63.md
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));

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.

これは把握していなかったので助かります。ありがとうございます。

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

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

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との対照性は気になりますね。ありがとうございます。

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.

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

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

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];
}
Comment on lines +83 to +88

Choose a reason for hiding this comment

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

処理がシンプルにまとまっていて,とても見やすいと思いました
参考にさせていただきます

}
}
return num_paths.back().back();
}
};
```