-
Notifications
You must be signed in to change notification settings - Fork 0
695. Max Area of Island #18
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ### step1 | ||
|
|
||
| 200. Number of Islandsを参考に、1のマスに当たるたびに隣接するマスを辿ってareaの大きさを計算する方針で実装。 | ||
|
|
||
| ### step2 | ||
|
|
||
| fという関数の名前をcountAreaに変更。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class Solution { | ||
| public: | ||
| int m, n; | ||
|
|
||
| void f(vector<vector<int>>& grid, int x, int y, int& val) { | ||
| if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0) { | ||
| return; | ||
| } | ||
| grid[x][y] = 0; | ||
| val++; | ||
| f(grid, x + 1, y, val); | ||
| f(grid, x - 1, y, val); | ||
| f(grid, x, y + 1, val); | ||
| f(grid, x, y - 1, val); | ||
| } | ||
|
|
||
| int maxAreaOfIsland(vector<vector<int>>& grid) { | ||
| m = grid.size(); | ||
| n = grid[0].size(); | ||
| int maxArea = 0; | ||
| for (int i = 0; i < m; i++) { | ||
| for (int j = 0; j < n; j++) { | ||
| if (grid[i][j] == 1) { | ||
| int val = 0; | ||
| f(grid, i, j, val); | ||
| maxArea = max(maxArea, val); | ||
| } | ||
| } | ||
| } | ||
| return maxArea; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class Solution { | ||
| public: | ||
| int m, n; | ||
|
|
||
| void countArea(vector<vector<int>>& grid, int x, int y, int& val) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. val という変数名は、中にどのような値が格納されているのか想像しづらく感じました。 area はいかがでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. areaの方がよさそうですね。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 出力を引数で参照渡しするほか、戻り値で返すこともできると思います。この辺りはチームの平均的な書き方に合わせることをお勧めいたします。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 承知しました! |
||
| if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0) { | ||
| return; | ||
| } | ||
| grid[x][y] = 0; | ||
| val++; | ||
| countArea(grid, x + 1, y, val); | ||
| countArea(grid, x - 1, y, val); | ||
| countArea(grid, x, y + 1, val); | ||
| countArea(grid, x, y - 1, val); | ||
| } | ||
|
|
||
| int maxAreaOfIsland(vector<vector<int>>& grid) { | ||
| m = grid.size(); | ||
| n = grid[0].size(); | ||
| int maxArea = 0; | ||
| for (int i = 0; i < m; i++) { | ||
| for (int j = 0; j < n; j++) { | ||
| if (grid[i][j] == 1) { | ||
| int val = 0; | ||
| countArea(grid, i, j, val); | ||
| maxArea = max(maxArea, val); | ||
| } | ||
| } | ||
| } | ||
| return maxArea; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class Solution { | ||
| public: | ||
| int m, n; | ||
|
|
||
| void countArea(vector<vector<int>>& grid, int x, int y, int& val) { | ||
| if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0) { | ||
| return; | ||
| } | ||
| grid[x][y] = 0; | ||
| val++; | ||
| countArea(grid, x + 1, y, val); | ||
| countArea(grid, x - 1, y, val); | ||
| countArea(grid, x, y + 1, val); | ||
| countArea(grid, x, y - 1, val); | ||
| } | ||
|
|
||
| int maxAreaOfIsland(vector<vector<int>>& grid) { | ||
| m = grid.size(); | ||
| n = grid[0].size(); | ||
| int maxArea = 0; | ||
| for (int i = 0; i < m; i++) { | ||
| for (int j = 0; j < n; j++) { | ||
| if (grid[i][j] == 1) { | ||
| int val = 0; | ||
| countArea(grid, i, j, val); | ||
| maxArea = max(maxArea, val); | ||
| } | ||
| } | ||
| } | ||
| return maxArea; | ||
| } | ||
| }; |
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.
関数名は UpperCamel で書くのをよく見かけます。この辺りはチームの平均的な書き方に合わせることをお勧めいたします。
参考までにスタイルガイドへのリンクを共有いたします。
https://google.github.io/styleguide/cppguide.html#Function_Names
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。