Skip to content

Graph bfs dfs 695#18

Open
hiroki-horiguchi-dev wants to merge 4 commits into
mainfrom
graph-bfs-dfs-695
Open

Graph bfs dfs 695#18
hiroki-horiguchi-dev wants to merge 4 commits into
mainfrom
graph-bfs-dfs-695

Conversation

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner

695. Max Area of Islandを解きました、レビューお願いします

Comment thread graph-bfs-dns/695.md
}

// find land
if (element == 1 && !visited[rowIndex][columnIndex]) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Step1 みたいに、探索の処理は関数に切り出した方が読みやすいと思いました

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.

そうですね、おっしゃる通りだと思います

Comment thread graph-bfs-dns/695.md
return maxAreaOfIslands;
}

private int countNumberOfIslands(int[][] grid, boolean[][] visited, int rowIndex, int columnIndex) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この関数名だと grid 内の島の数を数えているように見えました。 countAreaOfIsland などいかがでしょう?

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を数えているのでそちらの方が適切だと思います。ありがとうございます

Comment thread graph-bfs-dns/695.md
int element = grid[rowIndex][columnIndex];

// if element is not land or water.
if (element != 1 && element != 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0, 1は water, land のような定数にした方が扱いやすいと思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そうすれば if (element == land) のように書くことができ、可読性があがるので // if element is land. のようなコメントを削れると思います

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.

そうですね、ありがとうございます

Comment thread graph-bfs-dns/695.md

for (int rowIndex = 0; rowIndex < grid.length; rowIndex++) {
for (int columnIndex = 0; columnIndex < grid[0].length; columnIndex++) {
int element = grid[rowIndex][columnIndex];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

element という変数名には情報が乏しいと思いました(あらゆる配列の要素に関して使えてしまう)。grid の中のセルという意味で cell はどうでしょう?

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.

cell ありですね、サクサクっと書いていくと命名の精度が落ちてしまうので気をつけたいです。

Comment thread graph-bfs-dns/695.md
Comment on lines +121 to +128
// count top
if (previousRowIndex >= 0
&& grid[previousRowIndex][currentColumnIndex] == 1
&& !visited[previousRowIndex][currentColumnIndex]
) {
visited[previousRowIndex][currentColumnIndex] = true;
lands.add(new int[]{previousRowIndex,currentColumnIndex});
}
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回くり返し書くにはちょっと長く感じました。メソッドに切り出す、長さ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.

ありがとうございます、他の方のPR見てみます。

Copy link
Copy Markdown
Owner Author

@hiroki-horiguchi-dev hiroki-horiguchi-dev left a comment

Choose a reason for hiding this comment

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

皆さんありがとうございます。

Comment thread graph-bfs-dns/695.md
int element = grid[rowIndex][columnIndex];

// if element is not land or water.
if (element != 1 && element != 0) {
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.

そうですね、ありがとうございます

Comment thread graph-bfs-dns/695.md
return maxAreaOfIslands;
}

private int countNumberOfIslands(int[][] grid, boolean[][] visited, int rowIndex, int columnIndex) {
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を数えているのでそちらの方が適切だと思います。ありがとうございます

Comment thread graph-bfs-dns/695.md

for (int rowIndex = 0; rowIndex < grid.length; rowIndex++) {
for (int columnIndex = 0; columnIndex < grid[0].length; columnIndex++) {
int element = grid[rowIndex][columnIndex];
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.

cell ありですね、サクサクっと書いていくと命名の精度が落ちてしまうので気をつけたいです。

Comment thread graph-bfs-dns/695.md
}

// find land
if (element == 1 && !visited[rowIndex][columnIndex]) {
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.

そうですね、おっしゃる通りだと思います

Comment thread graph-bfs-dns/695.md
Comment on lines +121 to +128
// count top
if (previousRowIndex >= 0
&& grid[previousRowIndex][currentColumnIndex] == 1
&& !visited[previousRowIndex][currentColumnIndex]
) {
visited[previousRowIndex][currentColumnIndex] = true;
lands.add(new int[]{previousRowIndex,currentColumnIndex});
}
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.

ありがとうございます、他の方のPR見てみます。

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.

4 participants