Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 62_unique_paths/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [62. Unique Paths](https://leetcode.com/problems/unique-paths/description/)
17 changes: 17 additions & 0 deletions 62_unique_paths/step1.md
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名には、中に格納される値を表す英単語・英語句を付けたほうが、読み手にとって理解しやすくなると思います。 num_paths はいかがでしょうか?

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.

コメントありがとうございます。確かにgridでは中身の理解の助けにならないです。
num_pathsだと個人的にはpathsの数で単数形に聞こえてしまうので、pathsの数を複数格納していることをわかってもらえるようpath_countsにしようと思います。

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)$
18 changes: 18 additions & 0 deletions 62_unique_paths/step2.md
Original file line number Diff line number Diff line change
@@ -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)$
17 changes: 17 additions & 0 deletions 62_unique_paths/step3.md
Original file line number Diff line number Diff line change
@@ -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秒