Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/container-with-most-water/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
Sum of Two Integers

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、brute_force.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

int right = heights.size() - 1;
int max_amount_water = -1;
while (left < right) {
int height = min(heights[left], heights[right]);
Copy link

Choose a reason for hiding this comment

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

先に left または right を動かしてから、 max_amount_water を更新してもよいと思いました。ループの前に max_amount_water を、両端を壁にしたときの水の容量で初期化しておく必要はあります。

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.

2点気になったことをコメントしましたが、コードはとてもわかりやすかったです。

int maxArea(vector<int>& heights) {
int left = 0;
int right = heights.size() - 1;
int max_amount_water = -1;
Copy link

Choose a reason for hiding this comment

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

memo に書いてましたが、-1 での初期化は違和感がありました。0 がよいと思います

Comment on lines +4 to +6
if (heights.empty()) {
return 0;
}
Copy link

Choose a reason for hiding this comment

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

この部分がなくても heights が empty のときには 0 を返すように思いました。(while 文を通らないので初期値の 0 をそのまま返す)
そのまま処理しても問題がない場合は特殊処理を追加せず、うまく動かないケースのみをエッジケースとして特殊処理で弾くのがよいのではと個人的には思います。

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