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 みたいに、探索の処理は関数に切り出した方が読みやすいと思いました

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 などいかがでしょう?

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. のようなコメントを削れると思います

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 はどうでしょう?

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の配列を作りループする、など対処法がコメント集にあったと思うので、見ていなければ見てみると参考になるかもしれません。

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