Skip to content

695. Max Area of Island#19

Open
Ryotaro25 wants to merge 10 commits intomainfrom
problem18
Open

695. Max Area of Island#19
Ryotaro25 wants to merge 10 commits intomainfrom
problem18

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
695. Max Area of Island
https://leetcode.com/problems/max-area-of-island/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp, union_find.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

Copy link
Copy Markdown

@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.

関係のないファイルも差分に含まれているので、関係あるやつだけ差分に含めるようにして欲しいです

return row >= 0 && row < grid.size() && column >= 0 && column < grid[0].size();
}

int measure_island(vector<vector<int>>& grid, const int& row, const int& column, int& area) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはareaは参照で取り回すのではなく、帰り値で受け渡したいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@hayashi-ay
返り値で取り回すようにしました。
e1465f9

Comment on lines +22 to +23
measure_island(grid, row + 1, column, area);
measure_island(grid, row, column - 1, area);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

4方向について探索しないと正しく面積が出せないと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@hayashi-ay
コメントありがとうございます。
不要なファイルすみません。今回は削除→commitで対応しました。
4から2に出来ないかなと操作している作業中のものをPushしておりました🙇

別のコメントに関しては後ほど対応します。

public:
const int Water = 0;
const int Land = 1;
const int Visited = 2;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

PythonのPEP8との違いをふと気になって調べてみたのですが、
C++のGoogleのコーディング規約だと定数の頭にkをつけるみたいですね。
https://google.github.io/styleguide/cppguide.html#Constant_Names

何かしらの規約に従って命名されていれば問題なさそうですが、調べたので共有させていただきます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@Mike0121
レビューありがとうございます。なんとなく大文字はじまりをつかっておりました。
慣れないとk始まりには違和感ございますが、スタイルガイドに合わせて今後も実装しようと思います。

修正しました。
e1465f9

return row >= 0 && row < grid.size() && column >= 0 && column < grid[0].size();
}

int measure_island(vector<vector<int>>& grid, const int& row, const int& column, int& area) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

int はコピーしてもたいしたコストではないのでコンストリファレンスにする必要はないでしょう。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@oda
レビューありがとうございます。
返り値で取り回すような実装に変更しました。
e1465f9

const int Visited = 2;

bool is_inside(vector<vector<int>>& grid, const int& row, const int& column) {
return row >= 0 && row < grid.size() && column >= 0 && column < grid[0].size();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は個人的には小さい順に並べますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@oda
修正しました。
e1465f9


class Solution {
public:
const int Land = 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

定数を定義するとき、 constexpr を使うと C++11 っぽくなると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@nodchip
レビューありがとうございます。修正しました。
constexpr知りませんでした🙇
https://qiita.com/saltheads/items/dd65935878a0901fe9e7 も読んでみました。

e1465f9
882a21b

const int Visited = 2;

bool is_inside(vector<vector<int>>& grid, const int& row, const int& column) {
if ((0 <= row && row < grid.size()) && (0 <= column && column < grid[0].size())) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

インデントがずれているようです。

また、
return 0 <= row && row < grid.size() && 0 <= column && column < grid[0].size()
のほうがシンプルだと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@nodchip
インデント失礼しました。
書き方も合わせて修正しました。
882a21b

const int Land = 1;
const int Visited = 2;

bool is_inside(vector<vector<int>>& grid, const int& row, const int& column) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C++ の関数名は UpperCamel をお勧めいたします。

https://google.github.io/styleguide/cppguide.html#Function_Names

Ordinarily, functions should start with a capital letter and have a capital letter for each new word.

また、他のクラスから呼び出されない関数は、 private とするとよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@nodchip
うろ覚えの箇所は都度スタイルガイド読もうと思います🙇
882a21b


int maxAreaOfIsland(vector<vector<int>>& grid) {
int max_size = 0;
int row_size = grid.size();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

row_size だと、行の大きさを表しているように感じました。個人的には num_rows がよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

他の方針のものも合わせて修正しました。
882a21b


grid[row][column] = Visited;

int x_index = row * column_size + column;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

x と column がどちらも横方向の座標を表す単語のため、混乱しやすいように感じます。 x_index のほうをcell_indexy_index のほうを neighbor_cell_index などとしてはいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

xとyをきちんと使えていなかったですね。。。
修正しました。
882a21b

return x;
}

void unite(int x, int y) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

UnionFind で unite() という関数名は、あまり見ないように思います。 Union() という関数名をよく見かけます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!前回の問題からどっちだろうと気になっておりました。
882a21b

} else if (rank[root_x] < rank[root_y]) {
root[root_x] = root_y;
area[root_y] += area[root_x];
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここを } else if (root_x != root_y) { として、 is_connect() を削除し、呼び出し元では is_connect() を呼ばずに、いきなり unite() を呼び出したほうが、コードがシンプルになると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

uniteの最初にif(root_x == root_y) return をしちゃう方がシンプルだと自分は思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@nodchip @nittoco
お二人ともありがとうございます。
確かに呼び出しもとはいきなりUnionで、関数側で集合の代表が同じかどうかで処理に進むのか判断する方がスッキリですね。
882a21b

return area[find(x)];
}

bool is_connect(int x, int y) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

対象が複数あるため、 are_connect() のほうが良いかもしれません。

また、 be 動詞のあとの動詞は過去分詞形にして、 are_connected() にするとよいと思います。


int x_index = row * column_size + column;
for (const auto& direction : directions) {
int neighber_row = row + direction.dx;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

row は縦方向の座標で、 x は横方向の座標だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

こちら混同しておりました。修正しました。

return row >= 0 && row < grid.size() && column >= 0 && column < grid[0].size();
}

int measure_island(vector<vector<int>>& grid, const int& row, const int& column, int& size) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CPU の汎用レジスターのサイズ以下の値を参照経由で渡すと、アドレスの計算が行われるため、遅くなるとされています。値渡しのほうがよいと思います。

Effective C++ 第3版
https://www.maruzen-publishing.co.jp/item/b294734.html
20項 値渡しよりconst参照渡しを使おう

Effective C++ 第3版は一通り読まれることをお勧めいたします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@nodchip
以前紹介いただいて読み進めているのですが、まだ10項です...
しっかりと読み進めます🙇


int find(int x) {
while (x != root[x]) {
x = root[x];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rootってこの実装だとおそらく直接の親を指していますよね?
root_nodeは親を持たないnodeのことを指すので、ここでの命名はparentとかにした方がいい気がします
https://ja.wikipedia.org/wiki/%E6%9C%A8%E6%A7%8B%E9%80%A0_(%E3%83%87%E3%83%BC%E3%82%BF%E6%A7%8B%E9%80%A0)

Copy link
Copy Markdown
Owner Author

@Ryotaro25 Ryotaro25 Jul 18, 2024

Choose a reason for hiding this comment

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

@nittoco
ご指摘のとおり親です。資料もありがとうございます。
アルゴリズムイントロダクションを読み直したらそちらも親と記載ございました。。。修正しました🙇

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メンション先が間違っていますでしょうか…。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@nodchip 失礼しました。

Comment on lines +37 to +38
int area = 0;
max_area = max(measure_island(grid, row, column, area), max_area);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分ならこんな感じで書きます。

int area = measure_island(grid, row, column, 0);
max_area = max(area, max_area);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@hayashi-ay
返信遅くなりました。そうですね。0で初期化する意味ないですね。。。
step6で修正しました。
076f16b

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良いと思います。

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