560. Subarray Sum Equals K#16
Conversation
| return count | ||
| ``` | ||
|
|
||
| 回答を見た。 |
There was a problem hiding this comment.
https://discord.com/channels/1084280443945353267/1183683738635346001/1192145962479665304
https://discord.com/channels/1084280443945353267/1200089668901937312/1210956804226285578
https://discord.com/channels/1084280443945353267/1227073733844406343/1234063710377480234
https://discord.com/channels/1084280443945353267/1233603535862628432/1252232545056063548
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| count = 0 | ||
| cumulative_sum = 0 | ||
| prefix_sums = {0: 1} |
There was a problem hiding this comment.
ただの感想ですが、cumulative sum は prefix sum や inclusive scan, scan とも呼べるんですね。勉強になりました。
| class Solution: | ||
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| prefix_sums = defaultdict(int) | ||
| prefix_sums[0] = 1 |
There was a problem hiding this comment.
dictionary はユニークな key と value のペアを格納するものなので、key, value がどのようなものなのかを表す命名になっているとより分かりやすくなるように感じます。例えばこの場合は prefix_sum_to_frequency などでしょうか。
|
コメントされているところ以外気になるのはなかったです! |
| @@ -0,0 +1,93 @@ | |||
| 1st | |||
| 二重ループを思いついたが、TLE が発生。 | |||
There was a problem hiding this comment.
実行時間の見積もりについて私自身以前指摘されたので共有します
hroc135/leetcode#9 (comment)
| ```python | ||
| class Solution: | ||
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| prefix_sums = {0:1} |
There was a problem hiding this comment.
https://leetcode.com/problems/subarray-sum-equals-k/description/