Conversation
| } | ||
| } | ||
|
|
||
| return table_unique_paths.back().back(); |
| - $m-1$ か $n-1$ の小さい方を見る。 | ||
| - 計算量:時間 $O(\min(m, n))$(行数か列数の小さいほうでループするので)、空間 $O(1)$。 | ||
|
|
||
| 二重ループでインデックスを取る順番によってメモリ消費が変わるという話があった。ベクトルだと行方向に並んでいるので行を移動するとメモリを食う、みたいなこと? |
There was a problem hiding this comment.
メモリの使用量(消費)が異なることは稀だと思います。例えばGPUでのプログラミングでは、特定の単位(512バイトとか、正確な数字でないことをご容赦ください)ごとにパディングしてメモリを確保するので、このパディングされる容量によって総メモリ使用量が異なる、ということは有り得ます。一般的なCPUでこのようなパディングを含むメモリ確保をしている例はあまり聞いたことがありません。
アクセス速度については、参照先のスレッドにもあるように
一般に連続したメモリーへのアクセスの方が速いように設計されています
となっています。
There was a problem hiding this comment.
Goのスライスは、背後にあるメモリを余分に取ってるみたいですね。
https://zenn.dev/sryoya/articles/6a8ae7daa20aa7
There was a problem hiding this comment.
ありがとうございます。流し読みして勘違いしていたところ、詳細なコメントをいただきとても助かります。GPUの方の仕様は全く知らなかったので勉強になりました。
| class Solution { | ||
| public: | ||
| int uniquePaths(int m, int n) { | ||
| std::vector<std::vector<int>> table_unique_paths(m, std::vector<int>(n)); |
There was a problem hiding this comment.
std::vector<std::vector<int>> が table_ と同じ意味なので、変数名から取っても通じそうです。
| 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)); |
| 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]; | ||
| } |
There was a problem hiding this comment.
(単なる感想です)
多くの場合にこれらの if 条件を満たすので、どちらかというと例外の場合だけ分岐するようにしたい…と思いつつも、そっちはそっちで可読性が微妙だよなあ、と思いました。
| } | ||
| } | ||
|
|
||
| return table_unique_paths.back().back(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
std::vector 1 本でも計算できるのですが、やりすぎかもしれません。
問題
https://leetcode.com/problems/unique-paths/
次の問題
63. Unique Paths 2