Conversation
| if grid[i][j] == LAND: | ||
| if (i, j) in visited: |
There was a problem hiding this comment.
ここは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)] |
There was a problem hiding this comment.
個人的には[(1, 0), (-1, 0), (0, 1), (0, -1)]などの順番の方が意図が明確に感じます。好みの範囲かもしれません。
| n = len(grid[0]) | ||
| LAND = 1 | ||
| WATER = 0 | ||
| visited = set() |
| if grid[next_x][next_y] == WATER: | ||
| continue | ||
|
|
||
| area += 1 |
There was a problem hiding this comment.
pop後にインクリメントする方が自然な気がしますが、これも間違いではないですね。
| visited = set() | ||
| directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] | ||
| def calculate_area_of_island(x, y, area): | ||
| queue = deque() |
There was a problem hiding this comment.
queueはPythonの標準モジュール名と被るので自分は避けるようにしています。代わりにfrontiersとかはどうでしょうか?
There was a problem hiding this comment.
私は結構 queue, stack を使ってしまうのですが、あまり行儀がよくないのはそうですね。
grid_to_visit, cells_to_explore などですかね。
| visited = set() | ||
| directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] | ||
| def calculate_area_of_island(x, y, area): | ||
| queue = deque() |
There was a problem hiding this comment.
LeetCodeの実行環境では標準モジュール群がワイルドカードでimport済みなので問題にならないですが、本来はfrom collections import dequeが必要です。
| visited.add((x, y)) | ||
| area = 1 | ||
| while stack: | ||
| x, y = stack.pop() |
There was a problem hiding this comment.
好みの範囲ですが、x, yよりrow, colの方が自分は好みです。
There was a problem hiding this comment.
x, y を2次元配列のどの順序で並べるかという面倒な議論があります。
https://en.wikipedia.org/wiki/Row-_and_column-major_order
There was a problem hiding this comment.
パタヘネというコンピュータアーキテクチャの本を読んでおり、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): |
There was a problem hiding this comment.
グリッドを辿って面積を数えることをcalculateと表現するのは若干違和感があります。explore_island, traverse_islandとかはどうでしょうか?
There was a problem hiding this comment.
面積なので個人的には area があって欲しいですね。measure あたりですかねえ。calculate, compute あたりも私はありだと思います。
| return area | ||
| ``` | ||
|
|
||
| ## Step3 |
There was a problem hiding this comment.
同じロジックでも色々書き方はあると思います。以下ご参考までに。
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 | m = len(grid) | ||
| n = len(grid[0]) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
好みの問題かとは思いますが,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() |
解く問題
Max Area of Island
次に解く問題
Number of Connected Components In An Undirected Graph