63 unique paths ii - #33
Open
MA-yo-TA wants to merge 4 commits into
Open
Conversation
nodchip
reviewed
Aug 15, 2026
| 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)] |
There was a problem hiding this comment.
[[0] * num_columns for _ in range(num_rows)]のほうがシンプルだと思います。
There was a problem hiding this comment.
一応、このあたりのオブジェクトの理屈が分かっているかは確認しておいてください。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.wrv8idqm5j2b
Owner
Author
There was a problem hiding this comment.
お二人ともありがとうございます。確認しておきます。
| 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)] |
There was a problem hiding this comment.
空間計算量 O(num_columns) で解く方法もあります。興味があればぜひ考えてみて下さい。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。こうやれば良さそうというのが頭にあるのでそれを実装してみます。
MA-yo-TA
commented
Aug 21, 2026
| アイディア: | ||
|
|
||
| num_paths は各行を持つ必要はない。 | ||
| 各マスへの行き方は、そのマスの上のマスへの行き方と左のマスへの行き方の合計であるが、ある行を計算し終わったとき、次の行の各マスにおいて、上のマスからの寄与は今保持している行そのもの。左からの分だけ計算すればいい。 |
Owner
Author
There was a problem hiding this comment.
考えてみれば当たり前で、 step3 のやり方は
- 最初に 0 を詰める
- 上の行を
+=で足す
なので、そもそもコピーしていただけだったと考えると無駄なデータを保存していた。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
この問題:https://leetcode.com/problems/unique-paths-ii/
次の問題:https://leetcode.com/problems/house-robber/