diff --git a/62/memo.md b/62/memo.md new file mode 100644 index 0000000..1185805 --- /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..8cd94bf --- /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 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]