-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: TwoSum
Are you sure you want to change the base?
Conversation
num_subarrays += 1 | ||
return num_subarrays | ||
``` | ||
まずは総当たりでSubarrayを求めることを試みた.テストケースは突破したが,Submitの際,Output Limit Exceededとなってしまう. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Output Limit Exceeded はprint しているからですかね。
まずは総当たりを試みたとのことですが、まずは計算量を求め、そこから実行時間を推定することをお勧めします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
二重ループに加えてスライスをしているので3重ループしているのが時間制限を超えた主な原因だと私は思いました. printも遅くなる理由になると思います
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i, stepを混合して使っているので認知負荷が高いように思いました. 素直にiとjを使った方が読みやすいと感じました
There was a problem hiding this comment.
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となってしまう) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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とかでしょうか。
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for文の主語を、部分配列の個数にするんですね。
この方法は、思いついていませんでした! 勉強になります。
- 累積和を保持するためにHashmapを使うのが良さそう | ||
|
||
# Step3 | ||
他の人の解答を参考に解き直し |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私は解き直してもしなくてもなんでもいいと思っていますが、最終的には、何年後かに同じようなことをする必要がでたときに、10分くらいで解決できればいいという話かと思います。
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのif文は要らないように思います
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if文消しても同様の動作でした.ご指摘ありがとうございます.
Problem:
https://leetcode.com/problems/subarray-sum-equals-k/description/