From 4f1dd72d70b9da4cdee8af9dfb5974fb5527ed1e Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sat, 28 Mar 2026 07:46:21 +0900 Subject: [PATCH 1/2] 62. Unique Paths --- 62/memo.md | 39 +++++++++++++++++++++++++++++++++++++++ 62/sol1.py | 13 +++++++++++++ 62/sol2.py | 12 ++++++++++++ 62/sol3.py | 12 ++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 62/memo.md create mode 100644 62/sol1.py create mode 100644 62/sol2.py create mode 100644 62/sol3.py diff --git a/62/memo.md b/62/memo.md new file mode 100644 index 0000000..23d82e5 --- /dev/null +++ b/62/memo.md @@ -0,0 +1,39 @@ +# 62. Unique Paths + +- sol1.py + - 愚直に解いた。高校数学の問題として捉えコーディングの要素は考えていない + - mとnの大小関係を考慮した + - 計算量 O(min(m, n)) +- https://github.com/mamo3gr/arai60/blob/62_unique-paths/62_unique-paths/step1_dp.py + - DPで解く。メモ化再帰 + - https://docs.python.org/3.13/library/functools.html#functools.lru_cache + - 知らなかった +- https://github.com/mamo3gr/arai60/blob/62_unique-paths/62_unique-paths/step2.py + - メモ化再帰 + - 必要のないメモを削除して、メモリを削減していく過程がわかりやすい + - 最後のコードだけ真似する + +おまけ +- https://github.com/fuga-98/arai60/pull/33 + - リストの宣言法の違いなど +> リストを掛け算で長くすると中身が同じオブジェクトを指すことになるが、数字の場合は immutable で += などをしてもオブジェクトが作り直されるので大きな問題がないが、リストのように mutable なオブジェクトを掛け算で複製すると、実態はひとつなので、一つを変更するとすべてが変更されたように見える、ということです。 +- つまり、pythonのlistはオブジェクトのポインタを管理している。immutableなintなどを変更すると、新しいオブジェクトが作られてlistのポインタも別のアドレスに変わるため、そのオブジェクトだけが変わる。一方でmutableなlistなどを変更すると、そのオブジェクトが変更されてlistのポインタは変わらないため、そのポインタを格納しているlistの要素が全て変わったように見える。 + +```python +>>> a = [1]*10 +>>> a +[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] +>>> a[0] = 2 +>>> a +[2, 1, 1, 1, 1, 1, 1, 1, 1, 1] +>>> b = [[1]]*10 +>>> b[0][0] = 2 +>>> b +[[2], [2], [2], [2], [2], [2], [2], [2], [2], [2]] +>>> b = [[1] for _ in range(10)] +>>> b[0][0] = 2 +>>> b +[[2], [1], [1], [1], [1], [1], [1], [1], [1], [1]] +``` + + diff --git a/62/sol1.py b/62/sol1.py new file mode 100644 index 0000000..a9c78d5 --- /dev/null +++ b/62/sol1.py @@ -0,0 +1,13 @@ +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + total = 1 + if m < n: + m, n = n, m + + for i in range(m, m + n - 1): + total *= i + + for i in range(1, n): + total /= i + + return int(total) diff --git a/62/sol2.py b/62/sol2.py new file mode 100644 index 0000000..cfa841f --- /dev/null +++ b/62/sol2.py @@ -0,0 +1,12 @@ +from functools import lru_cache + + +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + @lru_cache(maxsize=None) + def count_unique_paths(a, b): + if a == 1 or b == 1: + return 1 + return count_unique_paths(a - 1, b) + count_unique_paths(a, b - 1) + + return count_unique_paths(m, n) diff --git a/62/sol3.py b/62/sol3.py new file mode 100644 index 0000000..6ea1bb0 --- /dev/null +++ b/62/sol3.py @@ -0,0 +1,12 @@ +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + if n < m: + m, n = n, m + + unique_paths_per_row = [1] * m + + for _ in range(1, n): + for col in range(1, m): + unique_paths_per_row[col] += unique_paths_per_row[col - 1] + + return unique_paths_per_row[-1] From 1124cf0573fae028cb9f0c1b7293e312f1a9bc11 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sat, 28 Mar 2026 12:01:59 +0900 Subject: [PATCH 2/2] Add suggested changes --- 62/memo.md | 2 +- 62/sol1.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/62/memo.md b/62/memo.md index 23d82e5..1185805 100644 --- a/62/memo.md +++ b/62/memo.md @@ -1,7 +1,7 @@ # 62. Unique Paths - sol1.py - - 愚直に解いた。高校数学の問題として捉えコーディングの要素は考えていない + - 高校数学の問題として捉えコーディングの要素は考えていない - mとnの大小関係を考慮した - 計算量 O(min(m, n)) - https://github.com/mamo3gr/arai60/blob/62_unique-paths/62_unique-paths/step1_dp.py diff --git a/62/sol1.py b/62/sol1.py index a9c78d5..8cd94bf 100644 --- a/62/sol1.py +++ b/62/sol1.py @@ -8,6 +8,6 @@ def uniquePaths(self, m: int, n: int) -> int: total *= i for i in range(1, n): - total /= i + total //= i - return int(total) + return total