Skip to content

Max Area of Island.md #17

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Max Area of Island.md #17

wants to merge 1 commit into from

Conversation

katataku
Copy link
Owner

@katataku katataku commented Jan 8, 2025

def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
WATER = 0
max_area = 0
seen = set()

Choose a reason for hiding this comment

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

二重配列にする選択肢もありますね。


- 書いた後の感想
- 昨日までは、「自分の中の最適値として暗記したコードを、出力している」という感覚が若干あった。(そのつもりはなかったけど、今日振り返るとそう感じる)
- 今日は、暗記というより「書くべきことを順次考えつつ、順次書く」という感じに変わった気がした。
Copy link

Choose a reason for hiding this comment

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

コードはいいと思います。

20-30問くらい解いたところで感想記事をネットに公開していただけると助かります。

row, col = islands.pop()
if (row, col) in seen:
continue
if not (0 <= row < len(grid) and 0 <= col < 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.

num_rows = len(grid)
num_cols = len(grid[0])

とおいておくと、 len(grid) と len(grid[0]) のどちらが縦でどちらが横か毎回考えずに済み、読みやすくなると思います。

class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
WATER = 0
max_area = 0
Copy link

Choose a reason for hiding this comment

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

宣言から利用まで距離がありますね。
宣言の場所を最後のfor文の直前にすると良いと思います。
変数について覚えておく必要のある距離が減り、記憶力が節約できてコードが読みやすくなります。

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

まあそんなに気になるところはなかったです。

islands.append((row - 1, col))
islands.append((row, col + 1))
islands.append((row, col - 1))
return num_area
Copy link

Choose a reason for hiding this comment

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

単にareaでもいいと思いました

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.

7 participants