Skip to content

63 unique paths ii - #33

Open
MA-yo-TA wants to merge 4 commits into
mainfrom
63-Unique-Paths-II
Open

63 unique paths ii#33
MA-yo-TA wants to merge 4 commits into
mainfrom
63-Unique-Paths-II

Conversation

@MA-yo-TA

Copy link
Copy Markdown
Owner

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)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[[0] * num_columns for _ in range(num_rows)]

のほうがシンプルだと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応、このあたりのオブジェクトの理屈が分かっているかは確認しておいてください。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.wrv8idqm5j2b

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.

お二人ともありがとうございます。確認しておきます。

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)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空間計算量 O(num_columns) で解く方法もあります。興味があればぜひ考えてみて下さい。

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.

ありがとうございます。こうやれば良さそうというのが頭にあるのでそれを実装してみます。

アイディア:

num_paths は各行を持つ必要はない。
各マスへの行き方は、そのマスの上のマスへの行き方と左のマスへの行き方の合計であるが、ある行を計算し終わったとき、次の行の各マスにおいて、上のマスからの寄与は今保持している行そのもの。左からの分だけ計算すればいい。

@MA-yo-TA MA-yo-TA Aug 21, 2026

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.

考えてみれば当たり前で、 step3 のやり方は

  1. 最初に 0 を詰める
  2. 上の行を += で足す

なので、そもそもコピーしていただけだったと考えると無駄なデータを保存していた。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants