Skip to content

560. Subarray Sum Equals K#16

Open
tarinaihitori wants to merge 1 commit into
mainfrom
560-subarray-sum-equals-k
Open

560. Subarray Sum Equals K#16
tarinaihitori wants to merge 1 commit into
mainfrom
560-subarray-sum-equals-k

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

return count
```

回答を見た。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

def subarraySum(self, nums: List[int], k: int) -> int:
count = 0
cumulative_sum = 0
prefix_sums = {0: 1}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ただの感想ですが、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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

dictionary はユニークな key と value のペアを格納するものなので、key, value がどのようなものなのかを表す命名になっているとより分かりやすくなるように感じます。例えばこの場合は prefix_sum_to_frequency などでしょうか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あまり Python に詳しくないので少し調べたのですが、このページでは上記例のような key_to_value といった書き方が人気みたいです。
Google の style guide にも、命名規則が載っているわけではないのですが、例として id_to_name_dict という命名が例として出されていますね。尚、これは _dict の部分は不要ですよということを示すための例ですので、それが含まれているという点に関しては良い命名ではないという点ご注意ください (参考)。

@nittoco
Copy link
Copy Markdown

nittoco commented Nov 14, 2024

コメントされているところ以外気になるのはなかったです!

@@ -0,0 +1,93 @@
1st
二重ループを思いついたが、TLE が発生。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

実行時間の見積もりについて私自身以前指摘されたので共有します
hroc135/leetcode#9 (comment)

```python
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
prefix_sums = {0:1}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

6 participants