Skip to content

695. Max Area of Island#34

Open
hayashi-ay wants to merge 3 commits intomainfrom
hayashi-ay-patch-23
Open

695. Max Area of Island#34
hayashi-ay wants to merge 3 commits intomainfrom
hayashi-ay-patch-23

Conversation

@hayashi-ay
Copy link
Copy Markdown
Owner

@hayashi-ay hayashi-ay changed the title Max Area of Island 695. Max Area of Island Feb 26, 2024
if grid[row][col] == WATER:
return 0
grid[row][col] = WATER
num_of_lands = 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

問題文中に land という単語が登場しておらず、違和感を覚えました。問題文中に登場する area を使用するのはいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.)

一応問題文中にもlandという単語はあるのですが、areaの方が良さそうです。

class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def count_num_of_lands_while_removing(row, col):
if not (0 <= row < height and 0 <= col < width):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LAND・WATER・height・width の定義が下のほうに書かれており、定義を確かめるため視線を上下に移動させる必要がありました。視線を上から下に移動するだけでコードが読めるよう、定義は使用箇所より上に書いたほうが良いと思います。ただし、必ずしもこの法則に当てはまるわけでないと思います。

return row * width + col

def union_connected_lands(row, col):
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

右方向とした方向のみ考慮すれば十分だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

気づかなかったです。ありがとうございます。

dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for dr, dc in dirs:
next_row, next_col = row + dr, col + dc
if not (0 <= next_row < height and 0 <= next_col < width):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

右方向とした方向のみ考慮すれば、 0<= next_row と 0 <= next_col の条件は消せると思います。

さらに、

        for h in range(height - 1):
            for w in range(width - 1):

とすれば、この if 文を消せると思います。

LAND = 1
WATER = 0
height, width = len(grid), len(grid[0])
seen = [ [False] * width for _ in range(height)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

seen で 1 度見たかどうかの判定は不要のように思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なくて良いですね。ありがとうございます。

max_area = 0
for h in range(height):
for w in range(width):
if grid[h][w] == LAND:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

calc_area_while_removing() の中で if grid[row][col] == WATER: をしているため、この条件は不要だと思います。

```python
class UnionFind:
def __init__(self, size):
self.groups = [i for i in range(size)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの範囲な気もしますが、[i for i in range(size)]はlist(range(size))でも良いように思いました。後者の方が単にlistにしたいだけだというのがすぐわかる気がします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。rangeでsequenceを生成してそのままlistに変換するのがシンプルでいいですね。

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.

3 participants