Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions 62/step1.cpp
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));
Copy link

Choose a reason for hiding this comment

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

grid という変数名ですと、 grid の要素が何を表すのか分かりにくく感じました。自分なら num_paths と付けると思います。

grid[0][0] = 1;
Copy link

Choose a reason for hiding this comment

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

個人の趣味ですが、grid.front().front() = 1; の方がより意図が明確になってよいかなと思います。

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];
Copy link

Choose a reason for hiding this comment

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

return grid.back().back(); の方が読み手にとってわかりやすいかなと思います。

}
};
22 changes: 22 additions & 0 deletions 62/step2_1.cpp
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;
Copy link

Choose a reason for hiding this comment

The 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) {
Copy link

Choose a reason for hiding this comment

The 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];
}
};
24 changes: 24 additions & 0 deletions 62/step2_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
単一の配列で実行するのを試す
*/
class Solution {

Choose a reason for hiding this comment

The 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);
Copy link

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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が最もわかりやすいと感じます。

Copy link

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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();
}
};
21 changes: 21 additions & 0 deletions 62/step3.cpp
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) {

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

これはあえて書かなくても読めるだろうと感じました。
下のif2つはどのみちif-continueでもif-elseでもないため条件を同時に読む必要があるからです。(下2つのifを同時に読むとrow == 0かつcol == 0のとき何もしないことは自然と読み取れます。)

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];
}
};