-
Notifications
You must be signed in to change notification settings - Fork 0
62. Unique Paths #46
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?
62. Unique Paths #46
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,28 @@ | ||
| // Time: 07:01 | ||
|
|
||
| // Time Space: O(m * n) | ||
| // Memory Space: O(m * n) | ||
|
|
||
| /* | ||
| 上と左の経路数をそのまま合計していき、それを繰り返すというのを思いつけたのでそのまま実装 | ||
| 変数名が若干分かりづらいので、grid、というコンテキストに適した名前にするとよさそう | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| int uniquePaths(int m, int n) { | ||
| vector<vector<int>> grid(m, vector<int>(n, 0)); | ||
| grid[0][0] = 1; | ||
|
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 i = 0; i < m; ++i) { | ||
| for (int j = 0; j < n; ++j) { | ||
| if (i > 0) { | ||
| grid[i][j] += grid[i-1][j]; | ||
| } | ||
| if (j > 0) { | ||
| grid[i][j] += grid[i][j-1]; | ||
| } | ||
| } | ||
| } | ||
| return grid[m-1][n-1]; | ||
|
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.
|
||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| 変数名を微修正 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int uniquePaths(int m, int n) { | ||
| vector<vector<int>> grid(m, vector<int>(n, 0)); | ||
| grid[0][0]=1; | ||
|
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 y = 0; y < m; ++y) { | ||
| for (int x = 0; x < n; ++x) { | ||
| if (x > 0) { | ||
| grid[y][x] += grid[y][x - 1]; | ||
| } | ||
| if(y > 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. if のあとにスペースを空けることをお勧めいたします。 |
||
| grid[y][x] += grid[y-1][x]; | ||
| } | ||
| } | ||
| } | ||
| return grid[m-1][n-1]; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| 単一の配列で実行するのを試す | ||
| */ | ||
| class Solution { | ||
|
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. これでいいかなと思います: class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> step_counter(n, 1);
for (int row = 1; row < m; ++row) {
for(int col = 1; col < n; ++col) {
step_counter[col] += step_counter[col - 1];
}
}
return step_counter.back();
}
}; |
||
| public: | ||
| int uniquePaths(int m, int n) { | ||
| vector<int> step_counter(n, 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. 格納されているものは経路の個数であり、ステップの個数ではないため、 step_counter と名付けるのは不自然に感じました。 |
||
| step_counter[0] = 1; | ||
|
|
||
| for (int y = 0; y < m; ++y) { | ||
| for(int x = 1; x < n; ++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. インクリメントの変数にy, xを使っているのがわかりにくいと感じました。(しかも行を表す1つ目がy, 列を表す2つ目がxなのも読みにくさを増やしていそうです。) 2D matrixのインデックスにはrow, colやi, jを使うことをおすすめします。自分はrow, colが最もわかりやすいと感じます。 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 step = 0; | ||
| if (y > 0) { | ||
| step += step_counter[x]; | ||
| } | ||
|
Comment on lines
+13
to
+15
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. step += step_counter[x]というyを使わない計算にy > 0という条件が必要な理由がわかりませんでした。なくても良さそうにみえます。 |
||
| if (x > 0) { | ||
| step += step_counter[x - 1]; | ||
| } | ||
|
Comment on lines
+16
to
+18
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. xは1から始まるのでif (x > 0)は不要に見えます。 |
||
| step_counter[x] = step; | ||
| } | ||
| } | ||
| return step_counter.back(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution { | ||
| public: | ||
| int uniquePaths(int m, int n) { | ||
| vector<vector<int>> step_count(m, vector<int>(n, 0)); | ||
| step_count[0][0] = 1; | ||
| for (int row = 0; row < m; ++row) { | ||
| for (int col = 0; col < n; ++col) { | ||
|
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. 略さずcolumnでも良いと思いました。 |
||
| if (col == 0 && row == 0) { | ||
| continue; | ||
| } | ||
|
Comment on lines
+8
to
+10
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 (row > 0) { | ||
| step_count[row][col] += step_count[row - 1][col]; | ||
| } | ||
| if (col > 0) { | ||
| step_count[row][col] += step_count[row][col - 1]; | ||
| } | ||
| } | ||
| } | ||
| return step_count[m - 1][n - 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.
grid という変数名ですと、 grid の要素が何を表すのか分かりにくく感じました。自分なら num_paths と付けると思います。