-
Notifications
You must be signed in to change notification settings - Fork 0
63. Unique Paths II #35
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
if obstacleGrid[0][col] == 0: | ||
paths[col] += paths[col - 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.
1 の後に 0 が続いた時も値が変わらない計算をしてしまうので、else break を入れるといいのかなと思いました。
また 1 の時に break すると、より分かりやすいように思いました。(OBSTACLE があったらもう0通りだね、ですとか、まだ定数を使っていないため、33行目のif文などと合わせて読むことを考えると、な気がしました。)
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.
for col in range(1, num_cols):
if obstacleGrid[0][col] == 1:
break
paths[col] += paths[col - 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.
ありがとうございます。
はい、そのイメージです。
if row == 0 and col == 0: | ||
continue |
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.
97行目の初期化と100から102行目のif文で十分伝わるのかなと感じたのですが、num_paths があるため、+直接 num_paths_table[row][col] に代入して というふうにもできるのかなと思いました。
if obstacleGrid[row][col] == Solution.OBSTACLE: | ||
num_paths_row[col] = 0 | ||
continue | ||
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.
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.
おっしゃってることってコードにするとどうなるでしょうか?イメージが上手く出来ず。
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 col != 0:
num_paths_row[col] += num_paths_row[col - 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://leetcode.com/problems/unique-paths-ii/description/