-
Notifications
You must be signed in to change notification settings - Fork 0
Add 695. Max Area of Island.md #19
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
NUM_ROW = len(grid) | ||
NUM_COLUMN = len(grid[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.
大文字を _ で繋いだものは定数で使うことが多いです。
column: int | ||
) -> int: | ||
area = 0 | ||
directions = ( |
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.
同じ意味のものが繰り返しているときには [] のほうが多い気がします。Tuple も immutable という点ではいいかもしれません。
return | ||
if self.rank[root1] > self.rank[root2]: | ||
self.parent[root2] = root1 | ||
self.size[root1] += self.size[root2] |
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.
私はここに return いれてしまいますね。
return max_area | ||
``` | ||
|
||
UnionFindで要素数記録 |
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.
共有までにと思いますが、UnionFindは、SWEの常識から微妙に外れるくらい、という感覚らしいです。
https://discord.com/channels/1084280443945353267/1183683738635346001/1197738650998415500
NUM_COLUMN = len(grid[0]) | ||
visited = [[False] * NUM_COLUMN for _ in range(NUM_ROW)] | ||
|
||
def is_explorable(row: int, column: int) -> 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.
この関数を入れるに至った意図などありましたら知りたいです。(説明見逃してたらすみません。)
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 先に進めない条件:
continue
が縦に続くよりは、
if 先に進める条件:
先に進む
みたいに書きたかったこともあります。
前者の書き方が続くと結局どういう条件で先に進めるのかよくわからなくなりそうな気持ちがあったので。
ただ、is_explorable()
の中では、先に進めない条件を確認してFalseを返すようにしてしまったので、やってることの意図と実際が噛み合ってなかったかもしれません。
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.
なるほどです。
前者の書き方は、早期終了の場合をさっさと終了・除去して、その後にメインの正常処理を詳細に書いていく、ということができるので、これなら割と直感的に書けるのではないかと感じました。
こちらのodaさんのコメント参考になるかもしれません。
https://discord.com/channels/1084280443945353267/1192728121644945439/1194203372115464272
next_positions = [(row, column)] | ||
while next_positions: | ||
row, column = next_positions.pop() | ||
visited[row][column] = 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.
forの中で行っているvisitedやWATERの判定を、whileの先頭で行ってearly return的な動きをさせるとコードが読みやすくなりそうです
その場合、next_positionsに無効なマスをいれることになるので、コードの効率性に対して若干のマイナスとなります
どちらを取るかは文脈次第ですがそういう考え方もあります。
max_area = 0 | ||
for row in range(NUM_ROW): | ||
for column in range(NUM_COLUMN): | ||
if is_explorable(row, column): |
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の範囲を考えると不要なので、無駄な処理をしていそうです。
visited = [[False] * NUM_COLUMN for _ in range(NUM_ROW)] | ||
for row in range(NUM_ROW): | ||
for column in range(NUM_COLUMN): | ||
if grid[row][column] == WATER: |
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文ですが、39行目でも同じことをしているのでメソッドとしてまとめるのもありかと思います
https://leetcode.com/problems/max-area-of-island/description/