Skip to content

Conversation

@jiku0730
Copy link
Owner

int current_max_area = 0;
for (int i = 0; i < heightSize; i++)
{
if ((heightSize - i) * height[i] >= current_max_area) {
Copy link

Choose a reason for hiding this comment

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

ネストが深く、やや読みにくく感じました。条件の真偽を逆にして、 continue したほうが読みやすくなると思います。

* [texthonda28の11番](https://github.com/thonda28/leetcode/pull/16)

左右を動かしていく方法がある。
まずは日本語で構造を整理する。
Copy link

Choose a reason for hiding this comment

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

念のため確認させてください。なぜこの方法で正しく動くか説明してみていただけますか?

@nodchip
Copy link

nodchip commented Jun 10, 2025

Pull Request に a.out も含まれてしまっているようです。レビューに不要なため、含めないほうがよいと思います。

{
int left = 0;
int right = heightSize - 1;
int current_max_area = (right - left) * min(height[left], height[right]);

Choose a reason for hiding this comment

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

細かいですが、 current の情報量が少ないため、
max_areaでも十分意味が伝わると思いました。

Choose a reason for hiding this comment

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

こちら僕も同意です

Copy link

@thonda28 thonda28 left a comment

Choose a reason for hiding this comment

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

C 言語は詳しくないのですが、返り値に () をつける流儀があるんですかね?個人的には冗長に感じたので () がないほうが見やすいと思いました。

Comment on lines +14 to +17
for (int i = 0; i < heightSize; i++)
{
if ((heightSize - i) * height[i] >= current_max_area) {
for (int j = heightSize - 1; j > i; j--)

Choose a reason for hiding this comment

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

全組合せを見るのであれば、よくあるシンプルな二重ループで見ていくのがわかりやすそうです。j を右端から見ていくことと if 文での判定を入れることでいくつかの area の計算をスキップできる嬉しさや途中での打ち切りができる嬉しさがあるということかと思いますが、その嬉しさよりも可読性がより優先度が高いかなと個人的には思います。

Suggested change
for (int i = 0; i < heightSize; i++)
{
if ((heightSize - i) * height[i] >= current_max_area) {
for (int j = heightSize - 1; j > i; j--)
for (int i = 0; i < heightSize; i++)
{
for (int j = i + 1; j < heightSize; j++)

{
if (height[j] >= height[i]) {
current_max_area = max(current_max_area, (j - i) * height[i]);
break ;

Choose a reason for hiding this comment

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

ここの break は memo.md に思考が書いてあったため意味がわかりましたが、コードだけを見たときにはコメント無しでは読み手が意図を汲み取るのに苦労しそうです。(ぱっと見でなぜこちらの分岐だけ打ち切っていいのかわかりませんでした)

{
int left = 0;
int right = heightSize - 1;
int current_max_area = (right - left) * min(height[left], height[right]);

Choose a reason for hiding this comment

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

こちら僕も同意です

Comment on lines +26 to +35
while (left < right)
{
if (height[left] < height[right]) {
left++;
}
else {
right--;
}
current_max_area = max (current_max_area, (right - left) * min(height[left], height[right]));
}

Choose a reason for hiding this comment

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

先に left, right を移動させてから計算を行うのが個人的には違和感がありました。おそらく初期化でした計算を再度したくなかったという意図ですかね?

ループは「今回の処理、次回の準備」のような流れで自然言語で説明できるコードが個人的にはわかりやすいと感じます。

current_max_area = max(current_max_area, (j - i) * height[i]);
break ;
}
else {
Copy link

Choose a reason for hiding this comment

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

この else 不要ですかね。

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.

6 participants