Skip to content

Conversation

@TORUS0818
Copy link
Owner

n = len(obstacleGrid[0])
num_ways = [[0] * n for _ in range(m)]

if obstacleGrid[0][0] == 1\
Copy link

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

Copy link
Owner Author

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
Copy link

Choose a reason for hiding this comment

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

この問題設定は、(0, 0) に障害物が来ることないんでしたっけ。

Copy link

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
Copy link

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 使うといいです。

Copy link

Choose a reason for hiding this comment

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

つまり、uniquePathsWithObstacles を2回呼んでも問題がないということです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
実験してみます。

Copy link
Owner Author

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

こんなイメージでしょうか。

Copy link

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)

Choose a reason for hiding this comment

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

m, nという命名よりかはnum_rowsnum_colsとかの方が読みやすいかなと思いました

Copy link
Owner Author

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:
Copy link

Choose a reason for hiding this comment

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

validは、何がvalidなのか読み取りづらかったです
通れる、のようなニュアンスがあるとわかりやすい気がします

Copy link

Choose a reason for hiding this comment

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

と思いましたが、これ通れなくてもtrueになることありますね(難しい)

Copy link
Owner Author

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:
Copy link

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]

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.

5 participants