From 0c69a8e80fe5792901b5f9d33ce2a82eb45eb272 Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Sat, 15 Aug 2026 16:18:54 +0900 Subject: [PATCH 1/4] step1 --- 63-Unique-Paths-II/note.md | 13 +++++++++++++ 63-Unique-Paths-II/step1.py | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 63-Unique-Paths-II/note.md create mode 100644 63-Unique-Paths-II/step1.py diff --git a/63-Unique-Paths-II/note.md b/63-Unique-Paths-II/note.md new file mode 100644 index 0000000..301bcf2 --- /dev/null +++ b/63-Unique-Paths-II/note.md @@ -0,0 +1,13 @@ +# 63. Unique Paths II + +https://leetcode.com/problems/unique-paths-ii/ + +## step1(まず通す) + +考え方は https://leetcode.com/problems/unique-paths/ と同様で、「あるのマスへの行き方はそのマスの左と上への行き方の合計」。ただし、障害物があるところはいけないので 0。 + +スタート地点に障害物がある場合の扱いは少し迷ったが、左上から右下までの生き方が存在しない = 0 と考えそのようにした。 + +## step2(整形&他の人のコードを読む) + +## step3(10分以内にさっとかける * 3回) diff --git a/63-Unique-Paths-II/step1.py b/63-Unique-Paths-II/step1.py new file mode 100644 index 0000000..3a4e213 --- /dev/null +++ b/63-Unique-Paths-II/step1.py @@ -0,0 +1,21 @@ +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid: list[list[int]]) -> int: + num_rows = len(obstacleGrid) + num_columns = len(obstacleGrid[0]) + num_paths = [[0 for _ in range(num_columns)] for _ in range(num_rows)] + if obstacleGrid[0][0] == 1: + return 0 + + num_paths[0][0] = 1 + for row in range(num_rows): + for column in range(num_columns): + if obstacleGrid[row][column] == 1: + num_paths[row][column] = 0 + continue + + if column + 1 < num_columns: + num_paths[row][column + 1] += num_paths[row][column] + if row + 1 < num_rows: + num_paths[row + 1][column] += num_paths[row][column] + + return num_paths[-1][-1] From 08320854aa1c04214d6622bb791592ec350149e7 Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Sat, 15 Aug 2026 16:31:19 +0900 Subject: [PATCH 2/4] step2 --- 63-Unique-Paths-II/note.md | 10 ++++++++++ 63-Unique-Paths-II/step2.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 63-Unique-Paths-II/step2.py diff --git a/63-Unique-Paths-II/note.md b/63-Unique-Paths-II/note.md index 301bcf2..9c91723 100644 --- a/63-Unique-Paths-II/note.md +++ b/63-Unique-Paths-II/note.md @@ -10,4 +10,14 @@ https://leetcode.com/problems/unique-paths-ii/ ## step2(整形&他の人のコードを読む) +https://github.com/h-masder/Arai60/pull/37 + +- ループの中でそのマスの左隣・下のインデックスが範囲内かをいちいち確認するとちょっと煩雑に見える可能性がある + - num_paths の 外側 1行1列を 0 埋めする + - 範囲チェックと後続処理をまとめて関数にする + +などが議論されている + +外側ゼロ埋めをやってみる。 + ## step3(10分以内にさっとかける * 3回) diff --git a/63-Unique-Paths-II/step2.py b/63-Unique-Paths-II/step2.py new file mode 100644 index 0000000..7029c75 --- /dev/null +++ b/63-Unique-Paths-II/step2.py @@ -0,0 +1,19 @@ +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid: list[list[int]]) -> int: + num_rows = len(obstacleGrid) + num_columns = len(obstacleGrid[0]) + num_paths = [[0 for _ in range(num_columns + 1)] for _ in range(num_rows + 1)] + if obstacleGrid[0][0] == 1: + return 0 + + num_paths[0][0] = 1 + for row in range(num_rows): + for column in range(num_columns): + if obstacleGrid[row][column] == 1: + num_paths[row][column] = 0 + continue + + num_paths[row][column + 1] += num_paths[row][column] + num_paths[row + 1][column] += num_paths[row][column] + + return num_paths[num_rows - 1][num_columns - 1] From b3f7e3b91c6de54ed11ae68a50c018e27b37af46 Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Sat, 15 Aug 2026 16:38:15 +0900 Subject: [PATCH 3/4] step3 --- 63-Unique-Paths-II/note.md | 3 +++ 63-Unique-Paths-II/step3.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 63-Unique-Paths-II/step3.py diff --git a/63-Unique-Paths-II/note.md b/63-Unique-Paths-II/note.md index 9c91723..51c7b41 100644 --- a/63-Unique-Paths-II/note.md +++ b/63-Unique-Paths-II/note.md @@ -21,3 +21,6 @@ https://github.com/h-masder/Arai60/pull/37 外側ゼロ埋めをやってみる。 ## step3(10分以内にさっとかける * 3回) + +- キャメルケースをやめ obstacle_grid に +- 1 は OBSTACLE という定数でおいて意味がわかりやすいように diff --git a/63-Unique-Paths-II/step3.py b/63-Unique-Paths-II/step3.py new file mode 100644 index 0000000..41dc0bc --- /dev/null +++ b/63-Unique-Paths-II/step3.py @@ -0,0 +1,20 @@ +class Solution: + def uniquePathsWithObstacles(self, obstacle_grid: list[list[int]]) -> int: + OBSTACLE = 1 + if obstacle_grid[-1][-1] == OBSTACLE: + return 0 + + num_rows = len(obstacle_grid) + num_columns = len(obstacle_grid[0]) + num_paths = [[0 for _ in range(num_columns + 1)] for _ in range(num_rows + 1)] + num_paths[0][0] = 1 + for row in range(num_rows): + for column in range(num_columns): + if obstacle_grid[row][column] == OBSTACLE: + num_paths[row][column] = 0 + continue + + num_paths[row][column + 1] += num_paths[row][column] + num_paths[row + 1][column] += num_paths[row][column] + + return num_paths[num_rows - 1][num_columns - 1] From 5bf426a09f6b67ebb6ff5c05052f19bdbc40ba96 Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Fri, 21 Aug 2026 17:02:14 +0900 Subject: [PATCH 4/4] =?UTF-8?q?step4=20=E7=A9=BA=E9=96=93=E8=A8=88?= =?UTF-8?q?=E7=AE=97=E9=87=8F=20O(num=5Fcolumns)=20=E3=81=AE=E3=82=84?= =?UTF-8?q?=E3=82=8A=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 63-Unique-Paths-II/note.md | 9 +++++++++ 63-Unique-Paths-II/step4.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 63-Unique-Paths-II/step4.py diff --git a/63-Unique-Paths-II/note.md b/63-Unique-Paths-II/note.md index 51c7b41..c9381f3 100644 --- a/63-Unique-Paths-II/note.md +++ b/63-Unique-Paths-II/note.md @@ -24,3 +24,12 @@ https://github.com/h-masder/Arai60/pull/37 - キャメルケースをやめ obstacle_grid に - 1 は OBSTACLE という定数でおいて意味がわかりやすいように + +## step4 + +空間計算量 O(num_colomuns) でやってみる + +アイディア: + +num_paths は各行を持つ必要はない。 +各マスへの行き方は、そのマスの上のマスへの行き方と左のマスへの行き方の合計であるが、ある行を計算し終わったとき、次の行の各マスにおいて、上のマスからの寄与は今保持している行そのもの。左からの分だけ計算すればいい。 diff --git a/63-Unique-Paths-II/step4.py b/63-Unique-Paths-II/step4.py new file mode 100644 index 0000000..c6a5cb5 --- /dev/null +++ b/63-Unique-Paths-II/step4.py @@ -0,0 +1,19 @@ +class Solution: + def uniquePathsWithObstacles(self, obstacle_grid: list[list[int]]) -> int: + OBSTACLE = 1 + if obstacle_grid[-1][-1] == OBSTACLE: + return 0 + + num_rows = len(obstacle_grid) + num_columns = len(obstacle_grid[0]) + num_paths = [0] * (num_columns + 1) + num_paths[0] = 1 + for row in range(num_rows): + for column in range(num_columns): + if obstacle_grid[row][column] == OBSTACLE: + num_paths[column] = 0 + continue + + num_paths[column + 1] += num_paths[column] + + return num_paths[num_columns - 1]