-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
def maxAreaOfIsland(self, grid: List[List[int]]) -> int: | ||
WATER = 0 | ||
max_area = 0 | ||
seen = set() |
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.
コードはいいと思います。
20-30問くらい解いたところで感想記事をネットに公開していただけると助かります。
row, col = islands.pop() | ||
if (row, col) in seen: | ||
continue | ||
if not (0 <= row < len(grid) and 0 <= col < 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.
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 |
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文の直前にすると良いと思います。
変数について覚えておく必要のある距離が減り、記憶力が節約できてコードが読みやすくなります。
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.
まあそんなに気になるところはなかったです。
islands.append((row - 1, col)) | ||
islands.append((row, col + 1)) | ||
islands.append((row, col - 1)) | ||
return num_area |
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.
単にareaでもいいと思いました
https://leetcode.com/problems/max-area-of-island/description/