-
Notifications
You must be signed in to change notification settings - Fork 0
695. Max Area of Island #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
- https://github.com/plushn/SWE-Arai60/pull/18#discussion_r2099839166 | ||
|
||
- https://github.com/plushn/SWE-Arai60/pull/18/files?short_path=2e957ae#diff-2e957aed1bb9dc5099abe16f37afb1a7980cfb2170d04ad76a48b4566e75eb35 | ||
- step1で再帰で実装するのを諦めたが際に, この方のstep1をみて実装のミスに気づいた.- [再帰のコールスタック制限を変更するsetrecursionlimit](https://docs.python.org/ja/3/library/sys.html#sys.setrecursionlimit)を見たが, 結局これを設定するのにはどうすれば良いのかわからない. とりあえず知識として持っておこうと思う |
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.
import sys
sys.setrecursionlimit(1000000)
みたいな感じじゃないでしょうか。
調べてみたところデフォルトは1000のようです(環境によっては違うかもしれません)
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.
ありがとうございます、そうですね書き方としてはそちらで良いと思いました。ただ、ここでコメントした"分からない"というのは、値をどの判断基準で設定するのかという点です(説明不足でした)。
例えば概算をしてみて、再帰がどれくらいになりそうだからデフォルトを超えそうなのでXと設定しようみたいにして使うんですかね。
class Solution: | ||
def maxAreaOfIsland(self, grid: List[List[int]]) -> int: | ||
WATER = 0 | ||
ISLAND = 1 |
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.
ISLANDではなくLANDのほうがいいかもしれません。ただ結局使われてないので定義しなくてもいいかもです。
def inside_island(row, column): | ||
return 0 <= row < num_rows and 0 <= column < num_columns | ||
|
||
def traverse_and_count_island(row, column): |
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)は測ったり計算するものであって数える(count)というイメージではないので、countよりもcalculate_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.
calculate は数式を用いて計算するというニュアンスを感じます。今回の場合は get_area() あたりが良いと思います。
|
||
while island: | ||
r, c = island.popleft() | ||
for (i, j) in ((1, 0), (-1, 0), (0, 1), (0, -1)): |
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.
iとjという名前についてですが、rとi、およびcとjの間に名前の直接的な関連性がないので、少しわかりにくいかもしれないと思いました。
drやdcなどの変数名にしてる人が多い気がしますが、両方1文字にしておかないと直後で何回も繰り返すので面倒という感じもします。
(r + i, c + j)のtuple自体をcellみたいな別の変数名に保存してしまって後の行で使う選択肢もあるかもしれません。
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.
r + i, c + j がこんだけ繰り返すならば変数に置きませんか。
あと、append_if_necessary みたいな関数を作って
append_if_necessary(r + 1, c) などと四回呼ぶ手もありますよ。
それと、私は i, j は避けるかもしれません。index らしさがあまりないですし、寿命の長い i, j はたまに事故を起こすので。
https://discord.com/channels/1084280443945353267/1367399154200088626/1375219482989166745
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.
そうですね、ここは改善の余地がありそうです。
コメントいただいたように変数として置き直すとかでも良いと思いました
|
||
while island: | ||
r, c = island.popleft() | ||
for (i, j) in ((1, 0), (-1, 0), (0, 1), (0, -1)): |
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.
r + i, c + j がこんだけ繰り返すならば変数に置きませんか。
あと、append_if_necessary みたいな関数を作って
append_if_necessary(r + 1, c) などと四回呼ぶ手もありますよ。
それと、私は i, j は避けるかもしれません。index らしさがあまりないですし、寿命の長い i, j はたまに事故を起こすので。
https://discord.com/channels/1084280443945353267/1367399154200088626/1375219482989166745
for (i, j) in ((1, 0), (-1, 0), (0, 1), (0, -1)): | ||
if not inside_island(r + i, c + j): | ||
continue | ||
elif grid[r + i][c + j] == WATER: |
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.
私はこれは if にしますね。elif にする理由がないので。
continue | ||
elif (i, j) in visited_island: | ||
continue | ||
area = traverse_island(area, i, j) |
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.
L45 を見て area = 0 ってなんだろうと思ってここまで読むと、L50 の引数に登場していました。あまり整理できていない段階でコードを書くと不要な部分が含まれることもありますが、書き上げた後、上からコードを読んでシミュレーションすることでコード整理までできれば良さそうと思いました。
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.
解くことに集中してしまって雑になってしまいました、そもそもここで定義する必要がないですね
def inside_island(row, column): | ||
return 0 <= row < num_rows and 0 <= column < num_columns | ||
|
||
def traverse_and_count_island(row, column): |
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.
好みだと思いますが、シンプルに count_island の方が意図が伝わりやすくなるのかなと思いました。
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.
count_island だと、面積ではなく島の数を数えているように感じられました。
def inside_island(i, j): | ||
return 0 <= i < num_rows and 0 <= j < num_columns | ||
|
||
def traverse_island(area, row, column): |
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 を引数とせずに、この関数内のローカル変数とする方法もあります。
以下の実装を見ると、この関数を呼び出したときは必ず area = 1 となります。
# step1 | ||
思考ログ | ||
- 前回のやつとかなり似ている問題 | ||
- dfsとbfsでどの場面でどう使い分けるのかにピンときていない. |
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.
あまり明確な基準ではないのですが、自分は以下のように使い分けています。
- dfs
- 答えが一つ見つかればよい場合
- 再帰関数でシンプルに書け、スタックオーバーフローしない場合
- bfs
- 最短経路を見つけたい場合
- 探索済みのノードの隣のノードの集合が必要になる場合
塗りつぶし等、上記のどれにも当てはまらない場合は、どちらでもよいと思います。その場合は気分で決めることが多いです。
def inside_island(row, column): | ||
return 0 <= row < num_rows and 0 <= column < num_columns | ||
|
||
def traverse_and_count_island(row, column): |
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.
calculate は数式を用いて計算するというニュアンスを感じます。今回の場合は get_area() あたりが良いと思います。
def inside_island(row, column): | ||
return 0 <= row < num_rows and 0 <= column < num_columns | ||
|
||
def traverse_and_count_island(row, column): |
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.
count_island だと、面積ではなく島の数を数えているように感じられました。
No description provided.