Open
Conversation
nodchip
reviewed
Jun 14, 2024
| class Solution: | ||
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| cumulative_sum = [0] * (len(nums) + 1) | ||
| cumulative_sum_to_index = defaultdict(list) |
There was a problem hiding this comment.
index が複数格納されるため、 cumulative_sum_to_indexes または cumulative_sum_to_indicies としたほうが良いと思います。
|
|
||
| count = 0 | ||
| for i in range(len(nums) + 1): | ||
| for j in cumulative_sum_to_index[cumulative_sum[i] - k]: |
There was a problem hiding this comment.
bisect で高速化できるとは思います。ですが、 phase2 の解法が正着のように思いますので、あまりこだわらなくても良いかもしれません。
Owner
Author
There was a problem hiding this comment.
あ, なるほど, 単調に増加するloop内部でcumulative_sum_to_index[cumulative_sum[i]]にappendされているのでsortされた状態になっているんですね
oda
reviewed
Jun 15, 2024
| hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/31/files | ||
| Exzrgs: https://github.com/Exzrgs/LeetCode/commit/caa03ede122e5e1d2de4b8ebd24ee26aaf3d743b | ||
|
|
||
| なんか前も先にindexを保存しておくかfor文の中で動的に処理するかみたいなので先にindexを保存したせいで沼にハマった問題をやった記憶があるな...? |
There was a problem hiding this comment.
Owner
Author
There was a problem hiding this comment.
maximum subarrayなども確かに同じ類ですね.....累積和だなと思ってそのまま書いたあとワンテンポ置いて動的に処理することで効率化できないか考えるようにでもしてみます
Owner
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
560. Subarray Sum Equals K