-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/63. Unique Paths II #34
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
- pathsを二次元で書いている | ||
- 直感的ではあるかもしれないが、不要なものを取っておくのは少し抵抗がある | ||
- 異常入力のエラーハンドリング | ||
- list[list[int]]であることは関数の引数で指定しているので、[[]]になっているケースだけ処理できれば良さそう |
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.
[[]]になっているケースだけ
どこまで考慮するかは別として、 []
みたいな空リストや [[1], [1,2]]
みたいに長さが揃ってないときとかはその型だけでは弾けてないですね
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | ||
num_rows = len(obstacleGrid) | ||
num_cols = len(obstacleGrid[0]) | ||
paths_to_row: list[int] = [1] + [0] * (num_cols-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.
- の前後にスペースを開けるのが一般的と思います。
https://peps.python.org/pep-0008/#other-recommendations
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.
むしろ開けない方が良いように読めました。先に計算される部分をくっつけておくことで分かりやすくする意図なのかなと思っています
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:
# Correct: i = i + 1 submitted += 1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b) # Wrong: i=i+1 submitted +=1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b)
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を誤読していました。
black というフォーマッタのルールではほぼ全ての演算子の前後にスペースを入れるようになっており、勘違いしていました。
https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-breaks-binary-operators
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | ||
num_rows = len(obstacleGrid) | ||
num_cols = len(obstacleGrid[0]) | ||
paths_to_row: list[int] = [1] + [0] * (num_cols-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.
paths_to_row だけではどのような変数か読み取ることができませんでした。例えば col_to_num_paths はいかがでしょうか。
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | ||
num_rows = len(obstacleGrid) | ||
num_cols = len(obstacleGrid[0]) | ||
paths_to_row = [1 if col == 0 else 0 for col in range(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.
paths_to_row = [0] * num_cols
paths_to_row[0] = 1
でよいと思いました。
問題文:https://leetcode.com/problems/unique-paths-ii/description/