Skip to content

695. Max Area of Island#23

Open
Yuto729 wants to merge 1 commit intomainfrom
max-area-of-island
Open

695. Max Area of Island#23
Yuto729 wants to merge 1 commit intomainfrom
max-area-of-island

Conversation

@Yuto729
Copy link
Owner

@Yuto729 Yuto729 commented Dec 27, 2025

@Yuto729 Yuto729 changed the title Max Area Of Island 695. Max Area of Island Dec 27, 2025
Repository owner deleted a comment from github-actions bot Dec 27, 2025
Comment on lines +43 to +44
if grid[i][j] == LAND:
if (i, j) in visited:

Choose a reason for hiding this comment

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

ここはif grid[i][j] == LAND and (i, j) not in visited:にしてネストを下げたいですね。

LAND = 1
WATER = 0
visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]

Choose a reason for hiding this comment

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

個人的には[(1, 0), (-1, 0), (0, 1), (0, -1)]などの順番の方が意図が明確に感じます。好みの範囲かもしれません。

n = len(grid[0])
LAND = 1
WATER = 0
visited = set()

Choose a reason for hiding this comment

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

girdと同じサイズの2D配列でも良いですね。

if grid[next_x][next_y] == WATER:
continue

area += 1
Copy link

@naoto-iwase naoto-iwase Dec 27, 2025

Choose a reason for hiding this comment

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

pop後にインクリメントする方が自然な気がしますが、これも間違いではないですね。

visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def calculate_area_of_island(x, y, area):
queue = deque()

Choose a reason for hiding this comment

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

queueはPythonの標準モジュール名と被るので自分は避けるようにしています。代わりにfrontiersとかはどうでしょうか?

https://docs.python.org/3/library/queue.html

Copy link

Choose a reason for hiding this comment

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

私は結構 queue, stack を使ってしまうのですが、あまり行儀がよくないのはそうですね。

grid_to_visit, cells_to_explore などですかね。

Copy link

Choose a reason for hiding this comment

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

nitですが,frontierと単数形にした方が英語的に適切だと思います(cf

visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def calculate_area_of_island(x, y, area):
queue = deque()

Choose a reason for hiding this comment

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

LeetCodeの実行環境では標準モジュール群がワイルドカードでimport済みなので問題にならないですが、本来はfrom collections import dequeが必要です。

visited.add((x, y))
area = 1
while stack:
x, y = stack.pop()

Choose a reason for hiding this comment

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

好みの範囲ですが、x, yよりrow, colの方が自分は好みです。

Copy link

Choose a reason for hiding this comment

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

x, y を2次元配列のどの順序で並べるかという面倒な議論があります。
https://en.wikipedia.org/wiki/Row-_and_column-major_order

Choose a reason for hiding this comment

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

パタヘネというコンピュータアーキテクチャの本を読んでおり、DGEMMのアセンブリ実装のあたりでちょうどそのトピックを見かけました!

行優先配置 (row major order): CやJavaで使用
列優先配置 (column major order): Fortranで使用

WATER = 0
visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def calculate_area_of_island(x, y):

Choose a reason for hiding this comment

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

グリッドを辿って面積を数えることをcalculateと表現するのは若干違和感があります。explore_island, traverse_islandとかはどうでしょうか?

Copy link

Choose a reason for hiding this comment

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

面積なので個人的には area があって欲しいですね。measure あたりですかねえ。calculate, compute あたりも私はありだと思います。

return area
```

## Step3
Copy link

@naoto-iwase naoto-iwase Dec 27, 2025

Choose a reason for hiding this comment

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

同じロジックでも色々書き方はあると思います。以下ご参考までに。

import copy
import collections


class Solution:
    def maxAreaOfIsland(self, grid: list[list[int]]) -> int:
        grid = copy.deepcopy(grid)
        num_rows = len(grid)
        num_cols = len(grid[0])
        WATER = 0
        LAND = 1

        def is_land(row, col):
            return (
                0 <= row < num_rows
                and 0 <= col < num_cols
                and grid[row][col] == LAND
            )
        
        def traverse(row_start, col_start):
            frontiers = collections.deque()
            
            def push_if_land(row, col):
                if is_land(row, col):
                    frontiers.append((row, col))
                    grid[row][col] = WATER
            
            push_if_land(row_start, col_start)
            area = 0
            while frontiers:
                row, col = frontiers.popleft()
                area += 1
                push_if_land(row + 1, col)
                push_if_land(row - 1, col)
                push_if_land(row, col + 1)
                push_if_land(row, col - 1)
            return area
        
        max_area = 0
        for row in range(num_rows):
            for col in range(num_cols):
                if is_land(row, col):
                    area = traverse(row, col)
                    max_area = max(max_area, area)
        return max_area   

Copy link

@t9a-dev t9a-dev left a comment

Choose a reason for hiding this comment

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

すでにコメントされている点以外で気になる点はありませんでした。

Comment on lines +10 to +11
m = len(grid)
n = len(grid[0])
Copy link

Choose a reason for hiding this comment

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

m, nだと変数の中身がわからなくなるので,num_row, num_colなどにする方が好みです.

WATER = 0
visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def calculate_area_of_island(x, y):
Copy link

Choose a reason for hiding this comment

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

x, yで座標のように指定していますが,リストの中にリストを入れて2次元を表現する際にはgrid[x][y]xが縦軸,yが横軸を指定するように対応させるケースが多いと思うので,混同しないようにコメントを入れても良いかもしれません.
つまり,

0 0 1 0
1 1 0 0
1 0 0 1

という縦3,横4のマップに対して

grid = [[0, 0, 1, 0],
        [1, 1, 0, 0],
        [1, 0, 0, 1]]

と対応させることが多く,grid[2][3]は「(0-originで)から2番目,から3番目」の要素と解釈することが多い気がするということです.
もちろんこれの転置をgridと置けばx, yという記号とスムーズに対応させることもできますが.

directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def calculate_area_of_island(x, y):
stack = [(x, y)]
visited.add((x, y))
Copy link

Choose a reason for hiding this comment

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

好みの問題かとは思いますが,visitedへのaddはstackからのpop直後に行うのが好みです.
stackへの追加時点ではvisitedしたわけではなくvisit先の候補として追加しただけで,実際にvisitするのはpopして取り出した瞬間だと解釈しているからです.

visited = set()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def calculate_area_of_island(x, y, area):
queue = deque()
Copy link

Choose a reason for hiding this comment

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

nitですが,frontierと単数形にした方が英語的に適切だと思います(cf

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