Skip to content

Create 62. Unique Paths.md#36

Open
irohafternoon wants to merge 2 commits intomainfrom
62.-Unique-Paths
Open

Create 62. Unique Paths.md#36
irohafternoon wants to merge 2 commits intomainfrom
62.-Unique-Paths

Conversation

@irohafternoon
Copy link
Copy Markdown
Owner

This Problem
Unique Paths
Next Problem
Unique Paths II

#### 感想
- 全て1で初期化する方法もあるが、上と左だけ1で、残りが0の方が実際のイメージには合致する
- けど、配列の初期化だったので、全て1で初期化して、コメントするのでもいいか
- dp配列の変数名は特に2次元配列だと難しい
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は、一番左上だけ1にして、上があったら上を足し、左があったら左を足す、というのが一番素直と思いますが、これは趣味の範囲です。

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.

ありがとうございます。
以下のようなイメージですね
おっしゃる通り初期化も含めて自然だと感じました

#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();
    }
};

- n, k がどれくらいの時にint型/long long 型に収まるのか、までは分からなかった。
- 答えがint型に収まるのであれば、計算途中はlong long 型には収まる気はする
- よく考えたら (n + 1 - i) / i は 常に1より大きいから計算中の値は単調増加か
- なので答えがある整数型に収まるなら、計算過程もその型に収まると言えそうだ
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.

最終的な結果に関してはこれで正しいのですが、計算途中で掛け算→割り算の順で行う必要があり、掛け算をした時点で超える可能性があります。訂正いたします

return coordinate_to_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.

特に問題ないと思います。

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.

3 participants