Skip to content

62. Unique Paths#33

Open
dxxsxsxkx wants to merge 1 commit into300_longest_increasing_subsequencefrom
62_unique_paths
Open

62. Unique Paths#33
dxxsxsxkx wants to merge 1 commit into300_longest_increasing_subsequencefrom
62_unique_paths

Conversation

@dxxsxsxkx
Copy link
Copy Markdown
Owner

}
}

return table_unique_paths.back().back();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすいです。

- $m-1$ か $n-1$ の小さい方を見る。
- 計算量:時間 $O(\min(m, n))$(行数か列数の小さいほうでループするので)、空間 $O(1)$。

二重ループでインデックスを取る順番によってメモリ消費が変わるという話があった。ベクトルだと行方向に並んでいるので行を移動するとメモリを食う、みたいなこと?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メモリの使用量(消費)が異なることは稀だと思います。例えばGPUでのプログラミングでは、特定の単位(512バイトとか、正確な数字でないことをご容赦ください)ごとにパディングしてメモリを確保するので、このパディングされる容量によって総メモリ使用量が異なる、ということは有り得ます。一般的なCPUでこのようなパディングを含むメモリ確保をしている例はあまり聞いたことがありません。

アクセス速度については、参照先のスレッドにもあるように

一般に連続したメモリーへのアクセスの方が速いように設計されています

となっています。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Goのスライスは、背後にあるメモリを余分に取ってるみたいですね。
https://zenn.dev/sryoya/articles/6a8ae7daa20aa7

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。流し読みして勘違いしていたところ、詳細なコメントをいただきとても助かります。GPUの方の仕様は全く知らなかったので勉強になりました。

class Solution {
public:
int uniquePaths(int m, int n) {
std::vector<std::vector<int>> table_unique_paths(m, std::vector<int>(n));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::vector<std::vector<int>>table_ と同じ意味なので、変数名から取っても通じそうです。

Suggested change
std::vector<std::vector<int>> table_unique_paths(m, std::vector<int>(n));
std::vector<std::vector<int>> unique_paths(m, std::vector<int>(n));

Comment on lines +10 to +15
if (row > 0) {
table_unique_paths[row][column] += table_unique_paths[row - 1][column];
}
if (column > 0) {
table_unique_paths[row][column] += table_unique_paths[row][column - 1];
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(単なる感想です)
多くの場合にこれらの if 条件を満たすので、どちらかというと例外の場合だけ分岐するようにしたい…と思いつつも、そっちはそっちで可読性が微妙だよなあ、と思いました。

}
}

return table_unique_paths.back().back();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

return table_unique_paths[m - 1][n - 1] より return table_unique_paths.back().back() の方が個人的に読みやすかったです。

class Solution {
public:
int uniquePaths(int m, int n) {
std::vector<int> num_paths_by_row(n, 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::vector 1 本でも計算できるのですが、やりすぎかもしれません。

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