Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
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内に各ステップで感じたことを追記します。

}

private:
static constexpr int NOANSWER = numeric_limits<int>::max();
Copy link

Choose a reason for hiding this comment

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

個人的には、これ定数で置かないほうが、上から読んでいって自然に理解できると思いますが、わりと趣味の範囲かなとは思います。

Copy link
Owner Author

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つの処理を纏めるより分けた方が好み。

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というの知らなかった。。。

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

ただ、そんなに一般的ではない、という反応を見た記憶もあります。

Copy link
Owner Author

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++) {
Copy link

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));

読みやすいかは微妙なところだと思います。

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.

5 participants