Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 695-max-area-of-island/memo.md
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回通すまで書き直し。
32 changes: 32 additions & 0 deletions 695-max-area-of-island/step1.cpp
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;
}
};
32 changes: 32 additions & 0 deletions 695-max-area-of-island/step2.cpp
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) {
Copy link
Copy Markdown

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

Ordinarily, functions follow PascalCase: start with a capital letter and have a capital letter for each new word.

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

val という変数名は、中にどのような値が格納されているのか想像しづらく感じました。 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.

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.

出力を引数で参照渡しするほか、戻り値で返すこともできると思います。この辺りはチームの平均的な書き方に合わせることをお勧めいたします。

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.

承知しました!

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;
}
};
32 changes: 32 additions & 0 deletions 695-max-area-of-island/step3.cpp
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;
}
};