-
Notifications
You must be signed in to change notification settings - Fork 0
63. Unique Paths II #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| n = len(obstacleGrid[0]) | ||
| num_ways = [[0] * n for _ in range(m)] | ||
|
|
||
| if obstacleGrid[0][0] == 1\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8 はこれ嫌がった気がします。
https://peps.python.org/pep-0008/#multiline-if-statements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。読み落としてました。
The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
この部分ですね。特にこだわりがあったわけではないので書き方を変更しようと思います。
with-statementsの場合は例外的なんですね。
| or obstacleGrid[-1][-1] == 1: | ||
| return 0 | ||
|
|
||
| num_ways[0][0] = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この問題設定は、(0, 0) に障害物が来ることないんでしたっけ。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、上で弾いていましたね。
| ```python | ||
| class Solution: | ||
| def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | ||
| @cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inner-function の場合は、毎回、別のオブジェクトとして生成されるので、この関数を2回呼んでも cache が再利用されないことを試して確認しておいてください。id 使うといいです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
つまり、uniquePathsWithObstacles を2回呼んでも問題がないということです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
実験してみます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def func():
def _inner_func():
pass
return _inner_func
if __name__ == '__main__':
f = func()
print(id(f)) # 4298894208
f = func()
print(id(f)) # 4298894352こんなイメージでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、そうです。そういうわけで、違うオブジェクトが作られます。
| or obstacleGrid[-1][-1] == OBSTACLE: | ||
| return 0 | ||
|
|
||
| m = len(obstacleGrid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m, nという命名よりかはnum_rows、num_colsとかの方が読みやすいかなと思いました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
今回は問題文に合わせて(合意がある前提で)書いてみたのですが、確かにrowとm、colとnどちらかに統一した方が見やすいなと思いました。
| class Solution: | ||
| def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | ||
| # 11:59 | ||
| def _check_if_valid_cell(row: int, col: int, is_transposed: bool) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validは、何がvalidなのか読み取りづらかったです
通れる、のようなニュアンスがあるとわかりやすい気がします
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
と思いましたが、これ通れなくてもtrueになることありますね(難しい)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。見直して手抜きだなと自分でも思いました。
下でご提案いただいているis_obstacleで良いかもしれませんね。
| for row in range(m): | ||
| for col in range(n): | ||
| if _check_if_valid_cell(row, col, is_transposed): | ||
| if col == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
趣味の範囲ですが、この方がちょっとすっきりしますかねえ
num_paths[0] = 1
for row in range(height):
for column in range(width):
if is_obstacle(row, column, is_transposed):
num_paths[column] = 0
continue
if column == 0:
continue
num_paths[column] += num_paths[column - 1]
https://leetcode.com/problems/unique-paths-ii/description/