Skip to content

Conversation

@colorbox
Copy link
Owner

@colorbox colorbox commented Dec 24, 2024

695. Max Area of Island

https://discord.com/channels/1084280443945353267/1183683738635346001/1195391359884988470

+++
current の位置を if の中にしませんか。
int Max はお行儀が悪いです。名前が関数と衝突するときには、max_size とか名前の付けた方のルールは変えないようにしましょう。snake とかそういうやつ。
                if(grid[i][j] == 0){
                    continue;
                }

のほうが私は好みですが、これはどちらでもいいでしょう。

int& count を引数にするのと return にするの、まあ、どちらでもいいですが、ええ、そうですね、こういうふうに考えてはどうでしょうか。

人が、各グリッドのマスに立っていて、全員がマニュアル(関数)を受け取ります。

で、(row, col) さんに電話をかけます。「いやー、悪いんだけどさ。島にいるよね。マニュアルに従って、その島塗りつぶしてくれない? ついでに、何人が塗りつぶしに協力したかも return して。」

そうするとですね。(row, col) さん、同じ内容の電話を周囲4マスにかけるんですよ。

で、そうすると、しばらくしてから、4方向からまた電話がかかってきます。
(衝突をさけるために、2回目以降の電話がかかってきた人は、もう塗ったと答えて0を返す。)

北「こっち、10でした。」
南「こっち、5でした。」
東「こっち、20でした。」
西「こっち、5でした。」
あとは自分。

はい。島のサイズは? 

https://discord.com/channels/1084280443945353267/1200089668901937312/1211642725594824704

+++
if not (0 <= row < len(grid) and 0 <= col < len(grid[row])):

にしたほうが、どこを走るかの意図は明らかですよね。
ただ、m, n はわりと主役級なので、width, height などの名前をつけてしまってもいいかもしれません。

Union Find のところ、
    def find(self, node):
        while self.groups[node] != node:
            self.groups[node] = self.find(self.groups[node])
        return self.groups[node]

ここは、再帰と while が合体していますが、どちらかでいいのでは?
これ、動きます?

row * m + col

これ、関数化してもいいかもしれません。location_id みたいな名前の。

fhiyo/leetcode#21
goto-untrapped/Arai60#31
kazukiii/leetcode#19
TORUS0818/leetcode#20
Yoshiki-Iwasa/Arai60#17
Mike0121/LeetCode#36
nittoco/leetcode#24
Ryotaro25/leetcode_first60#19
hroc135/leetcode#18
tarinaihitori/leetcode#18
YukiMichishita/LeetCode#6
rossy0213/leetcode#9
sakupan102/arai60-practice#19

while (!next_coordinates.empty()) {
auto [row, col] = next_coordinates.top();
next_coordinates.pop();
if (row < 0 || grid.size() <= row || col < 0 || grid.front().size() <= col) {
Copy link

Choose a reason for hiding this comment

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

個人的には不等号は、0 > row || row >= grid.size() と並べます。
意図は、0 <= row && row < grid.size() の否定であるというものです。趣味の範囲でしょう。

Copy link

Choose a reason for hiding this comment

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

自分は最近は !(0 <= row && row < grid.size() && 0<= col && col < grid.front().size()) と書くことが多くなりました。意図としては、不等号に常に小なりを使うことで、数直線上に一直線上に並んでいるように見せるのと、最小値と最大値で挟んでいるように見せることで、読み手の認知負荷を下げようとしているというものです。趣味の範囲だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。
しっくりする方でやっていこうと思います。

int maxAreaOfIsland(vector<vector<int>>& grid) {
int row_count = grid.size();
int col_count = grid[0].size();
vector<vector<bool>> visited(row_count, vector<bool>(col_count, false));
Copy link

Choose a reason for hiding this comment

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

個人的には set<pair<int, int>> もありかなと思います。少し遅いでしょうが少し考えることが減るかと。

Comment on lines +59 to +62
next_coordinates.emplace(row + 1, col);
next_coordinates.emplace(row - 1, col);
next_coordinates.emplace(row, col + 1);
next_coordinates.emplace(row, col - 1);
Copy link

Choose a reason for hiding this comment

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

stack に追加する前に範囲チェックをするのも一つです。
問題によっては計算量が変わることもあります。

そうすると、範囲チェックをして追加という、同じ処理が繰り返されるので関数化をしたりラムダにしたりするのがいいでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、ありがとうございます。
ラムダにするパターンもちょっと試してみようと思います。

int maxAreaOfIsland(vector<vector<int>>& grid) {
int row_count = grid.size();
int col_count = grid[0].size();
vector<vector<bool>> visited(row_count, vector<bool>(col_count, false));
Copy link

Choose a reason for hiding this comment

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

vector は特殊化されており、プロキシクラスが使われています。
https://cpprefjp.github.io/reference/vector/vector.html
そのため、動作がやや重い可能性があります。 vector を使うことをお勧めいたします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

vector<bool>の特殊化、知りませんでした、ありがとうございます。

}
visited[row][col] = true;
++area_count;
next_coordinates.emplace(row + 1, col);
Copy link

Choose a reason for hiding this comment

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

const int DELTA[][] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; といった配列を用意しておき、

for (int direction = 0; direction < 4; ++direction) {
    next_coordinates.emplace(row + DELTA[direction][0], col + DELTA[direction][1]);
}

などとしたほうが、シンプルになると思います。ただ、要素が 4 つしかなく、十分読みやすいため、現状のままでもよいかもしれません。

while (!next_coordinates.empty()) {
auto [row, col] = next_coordinates.top();
next_coordinates.pop();
if (row < 0 || grid.size() <= row || col < 0 || grid.front().size() <= col) {
Copy link

Choose a reason for hiding this comment

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

自分は最近は !(0 <= row && row < grid.size() && 0<= col && col < grid.front().size()) と書くことが多くなりました。意図としては、不等号に常に小なりを使うことで、数直線上に一直線上に並んでいるように見せるのと、最小値と最大値で挟んでいるように見せることで、読み手の認知負荷を下げようとしているというものです。趣味の範囲だと思います。

}

private:
int CountIslandArea(const int start_row, const int start_col, const vector<vector<int>>& grid, vector<vector<bool>>& visited) {
Copy link

Choose a reason for hiding this comment

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

自分は関数の引数のうち、関数への入力については、型がプリミティブ型の場合は const は付けず、 複合データ型の場合かつ入力の場合には付けるようにしています。このあたりはチームの平均的な書き方に合わせることをお勧めいたします。

@nittoco
Copy link

nittoco commented Dec 30, 2024

見ました!コメントされているところ以外は気になったところはありませんでした!

while (!next_coordinates.empty()) {
auto [row, col] = next_coordinates.top();
next_coordinates.pop();
if (row < 0 || grid.size() <= row || col < 0 || grid.front().size() <= col) {
Copy link

Choose a reason for hiding this comment

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

事前に関数内でrow_count, col_countを変数にするか引数として渡してもいいと思いました

@colorbox colorbox merged commit c3fce95 into main Feb 1, 2025
@colorbox colorbox deleted the 695 branch February 1, 2025 05:13
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.

6 participants