From 027f5bb6ffb5f85741355334fac6765cc3cd00ee Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Sat, 14 Sep 2024 08:20:48 +0900 Subject: [PATCH 1/5] create problem --- 62.UniquePaths/memo.md | 11 +++++++++++ 62.UniquePaths/step1.cpp | 6 ++++++ 62.UniquePaths/step2.cpp | 0 62.UniquePaths/step3.cpp | 0 4 files changed, 17 insertions(+) create mode 100644 62.UniquePaths/memo.md create mode 100644 62.UniquePaths/step1.cpp create mode 100644 62.UniquePaths/step2.cpp create mode 100644 62.UniquePaths/step3.cpp diff --git a/62.UniquePaths/memo.md b/62.UniquePaths/memo.md new file mode 100644 index 0000000..ea46127 --- /dev/null +++ b/62.UniquePaths/memo.md @@ -0,0 +1,11 @@ +## ステップ1 + + +## ステップ2 + + +## 他の解法 + + +## Discorなど + diff --git a/62.UniquePaths/step1.cpp b/62.UniquePaths/step1.cpp new file mode 100644 index 0000000..1eb55fc --- /dev/null +++ b/62.UniquePaths/step1.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + + } +}; diff --git a/62.UniquePaths/step2.cpp b/62.UniquePaths/step2.cpp new file mode 100644 index 0000000..e69de29 diff --git a/62.UniquePaths/step3.cpp b/62.UniquePaths/step3.cpp new file mode 100644 index 0000000..e69de29 From 6dbf8972d2dbf107fcd46eb49056b7d11799613d Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Thu, 7 Nov 2024 00:22:12 +0900 Subject: [PATCH 2/5] finish --- 62.UniquePaths/combitation.cpp | 18 ++++++++++++++++++ 62.UniquePaths/memo.md | 23 ++++++++++++++++++++--- 62.UniquePaths/step1.cpp | 10 +++++++++- 62.UniquePaths/step2.cpp | 14 ++++++++++++++ 62.UniquePaths/step3.cpp | 13 +++++++++++++ 62.UniquePaths/step4.cpp | 22 ++++++++++++++++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 62.UniquePaths/combitation.cpp create mode 100644 62.UniquePaths/step4.cpp diff --git a/62.UniquePaths/combitation.cpp b/62.UniquePaths/combitation.cpp new file mode 100644 index 0000000..cb2bf60 --- /dev/null +++ b/62.UniquePaths/combitation.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + // (m - 1) + (n - 1) = m + n - 2 + return combination(m + n - 2, min(m - 1, n - 1)); + } + +private: + int combination(int n, int r) { + long long result = 1; + + for (int i = 1; i <= r; i++) { + result = result * (n - i + 1) / i; + } + + return static_cast(result); + } +}; diff --git a/62.UniquePaths/memo.md b/62.UniquePaths/memo.md index ea46127..b0ee281 100644 --- a/62.UniquePaths/memo.md +++ b/62.UniquePaths/memo.md @@ -1,11 +1,28 @@ ## ステップ1 +問題を見て思いついたのは +(m + n)!/m!n!の公式を使う解法と1マスごとに、ロボットが何パターンで進むことができたのか記録する方法。 +今回は後者の方法で行う。右端に辿り着いた時点でのパターン数が解答になる。 +時間計算量O(m n) +空間計算量O(m n) ## ステップ2 +i jを使うかrow colを使うかは好みの問題か +空間計算量を抑えることはできる +## 参考にした他の方の解法 +基本的は方針は同じ +数学的な解き方でも解いてみる。C++に階乗やコンビネーションのライブラリはなさそう。 +https://github.com/Yoshiki-Iwasa/Arai60/pull/47 +combitation.cppに実装 +扱う数字の大きさを抑える方法が分からなかったのでChat GPTに相談 +combinationの第二引数には可能な限り小さい数値を渡すためにminを用いる +→たまたまLeetCodeのテストケースで通っただけのような気がする -## 他の解法 +行単位もしくは列単位でパターン数を記録することで空間計算量をO(m) もしくはO(n)に抑えることができる +ただ、コード面接でこれは思いつけそうにない +https://github.com/fhiyo/leetcode/pull/34 +step4.cppに実装(写経) - -## Discorなど +## Discordなど diff --git a/62.UniquePaths/step1.cpp b/62.UniquePaths/step1.cpp index 1eb55fc..7321696 100644 --- a/62.UniquePaths/step1.cpp +++ b/62.UniquePaths/step1.cpp @@ -1,6 +1,14 @@ class Solution { public: int uniquePaths(int m, int n) { - + vector> num_paths(m, vector(n, 1)); + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1]; + } + } + + return num_paths[m - 1][n - 1]; } }; diff --git a/62.UniquePaths/step2.cpp b/62.UniquePaths/step2.cpp index e69de29..9b7c4f4 100644 --- a/62.UniquePaths/step2.cpp +++ b/62.UniquePaths/step2.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> num_paths(m, vector(n, 1)); + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1]; + } + } + + return num_paths[m - 1][n - 1]; + } +}; \ No newline at end of file diff --git a/62.UniquePaths/step3.cpp b/62.UniquePaths/step3.cpp index e69de29..a069eff 100644 --- a/62.UniquePaths/step3.cpp +++ b/62.UniquePaths/step3.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> num_paths(m, vector(n, 1)); + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1]; + } + } + + return num_paths[m - 1][n - 1]; + } +}; diff --git a/62.UniquePaths/step4.cpp b/62.UniquePaths/step4.cpp new file mode 100644 index 0000000..a4f3758 --- /dev/null +++ b/62.UniquePaths/step4.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + // m と n を比較して、n の方が小さい場合、m と n を交換 + if (m < n) { + swap(m, n); + } + + vector num_paths(n, 1); + + // m - 1 回ループで行を更新 + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + // 左と上からの経路数を足す + num_paths[j] += num_paths[j - 1]; + } + } + + // 最後のセルのパス数を返す + return num_paths[n - 1]; + } +}; From 9cafa91c3be68b56b29172c0df074b5a18f87c15 Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Thu, 7 Nov 2024 22:35:59 +0900 Subject: [PATCH 3/5] reflrecting review comments --- 62.UniquePaths/combitation2.cpp | 18 ++++++++++++++++++ 62.UniquePaths/memo.md | 3 ++- 62.UniquePaths/step4.cpp | 2 +- 62.UniquePaths/step5.cpp | 21 +++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 62.UniquePaths/combitation2.cpp create mode 100644 62.UniquePaths/step5.cpp diff --git a/62.UniquePaths/combitation2.cpp b/62.UniquePaths/combitation2.cpp new file mode 100644 index 0000000..f8aec95 --- /dev/null +++ b/62.UniquePaths/combitation2.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + // (m - 1) + (n - 1) = m + n - 2 + return combination(m + n - 2, min(m - 1, n - 1)); + } + +private: + int combination(int n, int r) { + long long result = 1; + + for (int i = 1; i <= r; i++) { + result *= (n - i + 1) / i; + } + + return static_cast(result); + } +}; diff --git a/62.UniquePaths/memo.md b/62.UniquePaths/memo.md index b0ee281..a9306f0 100644 --- a/62.UniquePaths/memo.md +++ b/62.UniquePaths/memo.md @@ -12,7 +12,8 @@ i jを使うかrow colを使うかは好みの問題か ## 参考にした他の方の解法 基本的は方針は同じ -数学的な解き方でも解いてみる。C++に階乗やコンビネーションのライブラリはなさそう。 +~~数学的な解き方でも解いてみる。C++に階乗やコンビネーションのライブラリはなさそう。~~ +数学的な解き方でも解いてみる。C++に階乗やコンビネーションの標準ライブラリはなさそう。 https://github.com/Yoshiki-Iwasa/Arai60/pull/47 combitation.cppに実装 扱う数字の大きさを抑える方法が分からなかったのでChat GPTに相談 diff --git a/62.UniquePaths/step4.cpp b/62.UniquePaths/step4.cpp index a4f3758..322df03 100644 --- a/62.UniquePaths/step4.cpp +++ b/62.UniquePaths/step4.cpp @@ -1,7 +1,7 @@ class Solution { public: int uniquePaths(int m, int n) { - // m と n を比較して、n の方が小さい場合、m と n を交換 + // m と n を比較して、n の方が大きい場合、m と n を交換 if (m < n) { swap(m, n); } diff --git a/62.UniquePaths/step5.cpp b/62.UniquePaths/step5.cpp new file mode 100644 index 0000000..f52cc4b --- /dev/null +++ b/62.UniquePaths/step5.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> num_paths(m, vector(n)); + // 到達まで1パターンの部分を初期化 + for (int i = 0; i < m; i++) { + num_paths[i][0] = 1; + } + for (int j = 0; j < n; j++) { + num_paths[0][j] = 1; + } + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1]; + } + } + + return num_paths[m - 1][n - 1]; + } +}; From 16865f958401a91c79c90977f0f950567d2cc45e Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Wed, 20 Nov 2024 22:24:00 +0900 Subject: [PATCH 4/5] mod reflecting review --- 62.UniquePaths/combination3.cpp | 19 +++++++++++++++++++ 62.UniquePaths/memo.md | 13 +++++++++++++ 62.UniquePaths/step2.cpp | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 62.UniquePaths/combination3.cpp diff --git a/62.UniquePaths/combination3.cpp b/62.UniquePaths/combination3.cpp new file mode 100644 index 0000000..d3e9465 --- /dev/null +++ b/62.UniquePaths/combination3.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + // (m - 1) + (n - 1) = m + n - 2 + return combination(m + n - 2, min(m - 1, n - 1)); + } + +private: + int64_t combination(int64_t n, int64_t r) { + int64_t result = 1; + + for (int64_t i = 1; i <= r; i++) { + result *= (n - i + 1); + result /= i; + } + + return result; + } +}; diff --git a/62.UniquePaths/memo.md b/62.UniquePaths/memo.md index a9306f0..8ae7eae 100644 --- a/62.UniquePaths/memo.md +++ b/62.UniquePaths/memo.md @@ -27,3 +27,16 @@ step4.cppに実装(写経) ## Discordなど +## 多倍長整数クラス +64bitより長い数値を扱う計算全般を広義の多倍長(multiple precision)計算 + +https://na-inet.jp/weblog2/2018/03/22/%E3%81%9D%E3%82%82%E3%81%9D%E3%82%82%E3%80%8C%E5%A4%9A%E5%80%8D%E9%95%B7%E3%80%8D%E3%81%A3%E3%81%A6%E3%81%A9%E3%81%86%E3%81%84%E3%81%86%E6%84%8F%E5%91%B3%EF%BC%9F/ + +https://zenn.dev/herumi/articles/bitint-01-cpp + +Boost +https://ja.wikipedia.org/wiki/Boost_C%2B%2B%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA +cpp_int.hpp +https://github.com/boostorg/multiprecision/blob/develop/include/boost/multiprecision/cpp_int.hpp + +実装は難しかったので一周してから戻ってくる \ No newline at end of file diff --git a/62.UniquePaths/step2.cpp b/62.UniquePaths/step2.cpp index 9b7c4f4..7321696 100644 --- a/62.UniquePaths/step2.cpp +++ b/62.UniquePaths/step2.cpp @@ -11,4 +11,4 @@ class Solution { return num_paths[m - 1][n - 1]; } -}; \ No newline at end of file +}; From e9b5ef1bffadb946cfd43ded499d006cfae46cd1 Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Fri, 13 Jun 2025 13:15:26 +0900 Subject: [PATCH 5/5] finish --- 62.UniquePaths/recursive.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 62.UniquePaths/recursive.cpp diff --git a/62.UniquePaths/recursive.cpp b/62.UniquePaths/recursive.cpp new file mode 100644 index 0000000..bfaa029 --- /dev/null +++ b/62.UniquePaths/recursive.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + int uniquePaths(int row, int col) { + if (row == 1 || col == 1) { + return 1; + } + return uniquePaths(row - 1, col) + uniquePaths(row, col - 1); + } +};