-
Notifications
You must be signed in to change notification settings - Fork 0
695. Max Area of Island #32
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
Conversation
| while (!next_coordinates.empty()) { | ||
| auto [row, col] = next_coordinates.top(); | ||
| next_coordinates.pop(); | ||
| if (row < 0 || grid.size() <= row || col < 0 || grid.front().size() <= col) { |
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.
個人的には不等号は、0 > row || row >= grid.size() と並べます。
意図は、0 <= row && row < grid.size() の否定であるというものです。趣味の範囲でしょう。
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.
自分は最近は !(0 <= row && row < grid.size() && 0<= col && col < grid.front().size()) と書くことが多くなりました。意図としては、不等号に常に小なりを使うことで、数直線上に一直線上に並んでいるように見せるのと、最小値と最大値で挟んでいるように見せることで、読み手の認知負荷を下げようとしているというものです。趣味の範囲だと思います。
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.
コメントありがとうございます。
しっくりする方でやっていこうと思います。
| int maxAreaOfIsland(vector<vector<int>>& grid) { | ||
| int row_count = grid.size(); | ||
| int col_count = grid[0].size(); | ||
| vector<vector<bool>> visited(row_count, vector<bool>(col_count, false)); |
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.
個人的には set<pair<int, int>> もありかなと思います。少し遅いでしょうが少し考えることが減るかと。
| next_coordinates.emplace(row + 1, col); | ||
| next_coordinates.emplace(row - 1, col); | ||
| next_coordinates.emplace(row, col + 1); | ||
| next_coordinates.emplace(row, col - 1); |
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.
stack に追加する前に範囲チェックをするのも一つです。
問題によっては計算量が変わることもあります。
そうすると、範囲チェックをして追加という、同じ処理が繰り返されるので関数化をしたりラムダにしたりするのがいいでしょう。
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.
なるほど、ありがとうございます。
ラムダにするパターンもちょっと試してみようと思います。
| int maxAreaOfIsland(vector<vector<int>>& grid) { | ||
| int row_count = grid.size(); | ||
| int col_count = grid[0].size(); | ||
| vector<vector<bool>> visited(row_count, vector<bool>(col_count, false)); |
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.
vector は特殊化されており、プロキシクラスが使われています。
https://cpprefjp.github.io/reference/vector/vector.html
そのため、動作がやや重い可能性があります。 vector を使うことをお勧めいたします。
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.
vector<bool>の特殊化、知りませんでした、ありがとうございます。
| } | ||
| visited[row][col] = true; | ||
| ++area_count; | ||
| next_coordinates.emplace(row + 1, col); |
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.
const int DELTA[][] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; といった配列を用意しておき、
for (int direction = 0; direction < 4; ++direction) {
next_coordinates.emplace(row + DELTA[direction][0], col + DELTA[direction][1]);
}
などとしたほうが、シンプルになると思います。ただ、要素が 4 つしかなく、十分読みやすいため、現状のままでもよいかもしれません。
| while (!next_coordinates.empty()) { | ||
| auto [row, col] = next_coordinates.top(); | ||
| next_coordinates.pop(); | ||
| if (row < 0 || grid.size() <= row || col < 0 || grid.front().size() <= col) { |
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.
自分は最近は !(0 <= row && row < grid.size() && 0<= col && col < grid.front().size()) と書くことが多くなりました。意図としては、不等号に常に小なりを使うことで、数直線上に一直線上に並んでいるように見せるのと、最小値と最大値で挟んでいるように見せることで、読み手の認知負荷を下げようとしているというものです。趣味の範囲だと思います。
| } | ||
|
|
||
| private: | ||
| int CountIslandArea(const int start_row, const int start_col, const vector<vector<int>>& grid, vector<vector<bool>>& visited) { |
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.
自分は関数の引数のうち、関数への入力については、型がプリミティブ型の場合は const は付けず、 複合データ型の場合かつ入力の場合には付けるようにしています。このあたりはチームの平均的な書き方に合わせることをお勧めいたします。
|
見ました!コメントされているところ以外は気になったところはありませんでした! |
| while (!next_coordinates.empty()) { | ||
| auto [row, col] = next_coordinates.top(); | ||
| next_coordinates.pop(); | ||
| if (row < 0 || grid.size() <= row || col < 0 || grid.front().size() <= col) { |
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.
事前に関数内でrow_count, col_countを変数にするか引数として渡してもいいと思いました
695. Max Area of Island
https://discord.com/channels/1084280443945353267/1183683738635346001/1195391359884988470
+++
https://discord.com/channels/1084280443945353267/1200089668901937312/1211642725594824704
+++
fhiyo/leetcode#21
goto-untrapped/Arai60#31
kazukiii/leetcode#19
TORUS0818/leetcode#20
Yoshiki-Iwasa/Arai60#17
Mike0121/LeetCode#36
nittoco/leetcode#24
Ryotaro25/leetcode_first60#19
hroc135/leetcode#18
tarinaihitori/leetcode#18
YukiMichishita/LeetCode#6
rossy0213/leetcode#9
sakupan102/arai60-practice#19