-
Notifications
You must be signed in to change notification settings - Fork 0
209. Minimum Size Subarray Sum #53
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
| } | ||
|
|
||
| private: | ||
| static constexpr int NOANSWER = numeric_limits<int>::max(); |
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.
@oda
一つ覚えでやってしまってますね。。。
ありがとうございます。
|
|
||
| ## 他の方の解法 | ||
| leetcodeの解答例でsum += nums[r++];というふうにインクリメントしながらsumに追加する方法があった。 | ||
| 1行に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.
私も分けた方が分かりやすいなと思いました。
|
|
||
| Pythonのmath.infは型がfloat。言語が変わるとこの辺りの仕様は理解しておかないとバグに繋がりそう。 | ||
| 右側をループで回す方法で解いたが、左側を回す方法もあり。 | ||
| 余談、cumulative sumを略してcum sumというの知らなかった。。。 |
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.
numpyにもcumsumメソッドがあります。
https://numpy.org/doc/2.2/reference/generated/numpy.cumsum.html
ただ、そんなに一般的ではない、という反応を見た記憶もあります。
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 minSubArrayLen(int target, vector<int>& nums) { | ||
| vector<int> cumulative_sum(nums.size() + 1); | ||
| cumulative_sum[0] = 0; | ||
| for (int i = 0; i < nums.size(); 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.
std::partial_sum() も使えると思います。
vector<int> cumulative_sum(1);
std::partial_sum(nums.begin(), nums.end(), std::back_inserter(cumulative_sum));読みやすいかは微妙なところだと思います。
問題へのリンク
https://leetcode.com/problems/minimum-size-subarray-sum/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
Permutations
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step2_2.cpp、step3.cpp、cumulative_sum.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。