-
Notifications
You must be signed in to change notification settings - Fork 0
152. Maximum Product Subarray #73
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| ## 他の解法 | ||
| 負の数が偶数個あると最大になりうる特性を活かして途中でswapすることで | ||
| 自分のstep1で行った3パターンから2パターンへ比較回数を減らすことができる(step2_2で実装) |
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.
step 2 が私は愚直で気に入りました。このあたりで解いている人たちがいます。
https://discord.com/channels/1084280443945353267/1196498607977799853/1358736384604766238
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.
ありがとうございます。
小田さんの解法はstep2_2に近いので理解できました。
野田さんの解法は他のコメント、解説を読んでギリギリ理解できたような感覚です。きちんと言語化できないですが負の数が奇数になる場合を両サイドから見ていくことで避けていると理解しました。
| int max_product_so_far = nums[0]; | ||
| int min_product_so_far = nums[0]; | ||
| for (int i = 1; i < nums.size(); i++) { | ||
| int temp_max_so_far = max(nums[i], max(max_product_so_far * nums[i], min_product_so_far * nums[i])); |
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.
Python のようにしたければ
tie(a, b) = make_pair(b, a);というのが一応ありますが、素直に両方の変数を new_max, new_min とでも一度置いてから更新のほうが読みやすいでしょう。
|
|
||
| return max_product; | ||
| } | ||
| }; No newline at end of file |
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.
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.
@Apo-Matchbox
確認有り難とうございます。漏れておりました。。。

問題へのリンク
https://leetcode.com/problems/maximum-product-subarray/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
15. 3Sum
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step2_2.cpp、step3.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。