diff --git a/62_unique_paths/problem.md b/62_unique_paths/problem.md new file mode 100644 index 0000000..3119501 --- /dev/null +++ b/62_unique_paths/problem.md @@ -0,0 +1 @@ +## 問題: [62. Unique Paths](https://leetcode.com/problems/unique-paths/description/) diff --git a/62_unique_paths/step1.md b/62_unique_paths/step1.md new file mode 100644 index 0000000..3847198 --- /dev/null +++ b/62_unique_paths/step1.md @@ -0,0 +1,17 @@ +# Step 1 + +- ある地点$grid[i][j]$に辿り着けるルートの数は$grid[i - 1][j]$に辿り着くルートの数と$grid[i][j - 1]$に辿り着くルートの数の和である + +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + grid = [[1] * n] + [[1] + [0] * (n - 1)] * (m - 1) + for i in range(1, m): + for j in range(1, n): + grid[i][j] = grid[i - 1][j] + grid[i][j - 1] + return grid[m - 1][n - 1] +``` + +時間計算量: $O(m \times n)$ + +空間計算量: $O(m \times n)$ diff --git a/62_unique_paths/step2.md b/62_unique_paths/step2.md new file mode 100644 index 0000000..c8177ee --- /dev/null +++ b/62_unique_paths/step2.md @@ -0,0 +1,18 @@ +# Step 2 + +- 空間計算量を改善する + +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + row = [1] * n + for i in range(1, m): + for j in range(1, n): + row[j] += row[j - 1] + return row[n - 1] + +``` + +時間計算量: $O(m \times n)$ + +空間計算量: $O(n)$ diff --git a/62_unique_paths/step3.md b/62_unique_paths/step3.md new file mode 100644 index 0000000..9a64dfa --- /dev/null +++ b/62_unique_paths/step3.md @@ -0,0 +1,17 @@ +# Step 3 + +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + row = [1] * n + for i in range(1, m): + for j in range(1, n): + row[j] += row[j - 1] + return row[n - 1] +``` + +1回目: 1分 20秒 + +2回目: 1分 6秒 + +3回目: 1分 6秒