Open
Conversation
oda
reviewed
May 6, 2025
| #### 感想 | ||
| - 全て1で初期化する方法もあるが、上と左だけ1で、残りが0の方が実際のイメージには合致する | ||
| - けど、配列の初期化だったので、全て1で初期化して、コメントするのでもいいか | ||
| - dp配列の変数名は特に2次元配列だと難しい |
There was a problem hiding this comment.
私は、一番左上だけ1にして、上があったら上を足し、左があったら左を足す、というのが一番素直と思いますが、これは趣味の範囲です。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
以下のようなイメージですね
おっしゃる通り初期化も含めて自然だと感じました
#include <vector>
class Solution {
public:
int uniquePaths(int m, int n) {
std::vector<std::vector<int>> coordinate_to_paths(m, std::vector<int>(n));
coordinate_to_paths[0][0] = 1;
for (int row = 0; row < m; row++) {
for (int column = 0; column < n; column++) {
if (row > 0) {
coordinate_to_paths[row][column] += coordinate_to_paths[row - 1][column];
}
if (column > 0) {
coordinate_to_paths[row][column] += coordinate_to_paths[row][column - 1];
}
}
}
return coordinate_to_paths.back().back();
}
};
irohafternoon
commented
May 6, 2025
| - n, k がどれくらいの時にint型/long long 型に収まるのか、までは分からなかった。 | ||
| - 答えがint型に収まるのであれば、計算途中はlong long 型には収まる気はする | ||
| - よく考えたら (n + 1 - i) / i は 常に1より大きいから計算中の値は単調増加か | ||
| - なので答えがある整数型に収まるなら、計算過程もその型に収まると言えそうだ |
Owner
Author
There was a problem hiding this comment.
最終的な結果に関してはこれで正しいのですが、計算途中で掛け算→割り算の順で行う必要があり、掛け算をした時点で超える可能性があります。訂正いたします
Fuminiton
reviewed
May 7, 2025
| return coordinate_to_paths.back().back(); | ||
| } | ||
| }; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This Problem
Unique Paths
Next Problem
Unique Paths II