Skip to content

Create Subarray Sum Equals K.md #6

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

Open
wants to merge 1 commit into
base: TwoSum
Choose a base branch
from

Conversation

aiueoriku
Copy link
Owner

num_subarrays += 1
return num_subarrays
```
まずは総当たりでSubarrayを求めることを試みた.テストケースは突破したが,Submitの際,Output Limit Exceededとなってしまう.

Choose a reason for hiding this comment

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

Output Limit Exceeded はprint しているからですかね。
まずは総当たりを試みたとのことですが、まずは計算量を求め、そこから実行時間を推定することをお勧めします。

Choose a reason for hiding this comment

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

二重ループに加えてスライスをしているので3重ループしているのが時間制限を超えた主な原因だと私は思いました. printも遅くなる理由になると思います

Copy link
Owner Author

Choose a reason for hiding this comment

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

2重ループ,スライス,printなど好き放題処理を書いていましたが,処理効率をもう少し考えられるように頑張ります.ご指摘ありがとうございます.

for steps in range(len(nums)):
for i in range(len(nums)-steps):
print(f"nums[i:i+steps+1]:{nums[i:i+steps+1]}")
if sum(nums[i:i+steps+1]) == k:

Choose a reason for hiding this comment

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

好みかもしれませんがスペースを入れると読みやすくなると思いました。
sum(nums[i : i + steps + 1]) == k
https://peps.python.org/pep-0008/#other-recommendations

Comment on lines +7 to +10
for steps in range(len(nums)):
for i in range(len(nums)-steps):
print(f"nums[i:i+steps+1]:{nums[i:i+steps+1]}")
if sum(nums[i:i+steps+1]) == k:

Choose a reason for hiding this comment

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

i, stepを混合して使っているので認知負荷が高いように思いました. 素直にiとjを使った方が読みやすいと感じました

Copy link
Owner Author

Choose a reason for hiding this comment

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

変数に意味を持たせたくstepを採用したのですが,逆効果だったということですね.ありがとうございます.

prefix_sum[current_sum] += 1
return subarray_count
```
prefix_sum[0]=1を書かないと,例えばnums=[1,2,3], k=1で最初の要素がヒットするときに対応出来ないというポイントがある.(prefix_sum[complement]=0となってしまう)
Copy link

Choose a reason for hiding this comment

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

class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
subarray_count = 0
current_sum = 0
Copy link

@potrue potrue May 23, 2025

Choose a reason for hiding this comment

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

current_sumという変数名思いつきませんでしたが結構いいですね。
ただ、prefix_sumという名前に対応させる場合はcurrent_prefix_sumとかにしておいたほうがいいかもしれません。

def subarraySum(self, nums: List[int], k: int) -> int:
subarray_count = 0
current_sum = 0
prefix_sum = defaultdict(int)
Copy link

Choose a reason for hiding this comment

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

prefix_sumだけだとvalueに値に何が入ってるかわからないので、累積和が出現する回数が入ってますよ、というのが何かしらわかる変数名にしたい気がします。sum_appearance_countとかでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

sum_appearance_countいいですね.変数名の選定にも慣れていきたいですね.

何も見ないで解く
```python
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:

Choose a reason for hiding this comment

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

for文の主語を、部分配列の個数にするんですね。
この方法は、思いついていませんでした! 勉強になります。

- 累積和を保持するためにHashmapを使うのが良さそう

# Step3
他の人の解答を参考に解き直し

Choose a reason for hiding this comment

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

コメントをもらった際に、書き直しのステップを追加した方が良いという話があります。

https://discord.com/channels/1084280443945353267/1196498607977799853/1233081465072390164

Copy link

Choose a reason for hiding this comment

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

私は解き直してもしなくてもなんでもいいと思っていますが、最終的には、何年後かに同じようなことをする必要がでたときに、10分くらいで解決できればいいという話かと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます.一応,自分はStep1:何も見ずに解く.の段階で,答えなど参照しつつ,最後には何も見ずに1から書けるまで作業するようにしています.ただ時間までは気にしていなかったので,気に掛けることができればより実践的かもしれないです.

コメントをもらった際に、書き直しのステップを追加した方が良いという話があります。

https://discord.com/channels/1084280443945353267/1196498607977799853/1233081465072390164

for num in nums:
current_sum += num
complement = current_sum - k
if complement in prefix_sum:

Choose a reason for hiding this comment

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

ここのif文は要らないように思います

Copy link
Owner Author

Choose a reason for hiding this comment

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

if文消しても同様の動作でした.ご指摘ありがとうございます.

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.

7 participants