-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Step1 | ||
何も見ないで解く | ||
```python | ||
class Solution: | ||
def subarraySum(self, nums: List[int], k: int) -> int: | ||
num_subarrays = 0 | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 好みかもしれませんがスペースを入れると読みやすくなると思いました。
Comment on lines
+7
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 変数に意味を持たせたくstepを採用したのですが,逆効果だったということですね.ありがとうございます. |
||
num_subarrays += 1 | ||
return num_subarrays | ||
``` | ||
まずは総当たりでSubarrayを求めることを試みた.テストケースは突破したが,Submitの際,Output Limit Exceededとなってしまう. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 2重ループ,スライス,printなど好き放題処理を書いていましたが,処理効率をもう少し考えられるように頑張ります.ご指摘ありがとうございます. |
||
|
||
# Step2 | ||
他の人の解答を見る. | ||
https://github.com/rinost081/LeetCode/pull/15/ | ||
|
||
- 累積和がキーワードぽい.今の総和-累積和をすることで1回のfor文で事足りる. | ||
- 累積和を保持するためにHashmapを使うのが良さそう | ||
|
||
# Step3 | ||
他の人の解答を参考に解き直し | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. ご指摘ありがとうございます.一応,自分はStep1:何も見ずに解く.の段階で,答えなど参照しつつ,最後には何も見ずに1から書けるまで作業するようにしています.ただ時間までは気にしていなかったので,気に掛けることができればより実践的かもしれないです.
|
||
```python | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. current_sumという変数名思いつきませんでしたが結構いいですね。 |
||
prefix_sum = defaultdict(int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. sum_appearance_countいいですね.変数名の選定にも慣れていきたいですね. |
||
prefix_sum[0] = 1 | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. if文消しても同様の動作でした.ご指摘ありがとうございます. |
||
subarray_count += prefix_sum[complement] | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. |
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文の主語を、部分配列の個数にするんですね。
この方法は、思いついていませんでした! 勉強になります。