Conversation
There was a problem hiding this comment.
defaultdict を使っているので、この2行は単に(if 文は省略して) subarray_count += prefix_sum_to_count[prefix_sum - k] と書けます。
追記:「この2行」とは
if prefix_sum - k in prefix_sum_to_count:
subarray_count += prefix_sum_to_count[prefix_sum - k]のことです。github上だとどこを指しているのかわかりにくかったので追記しました🙏
There was a problem hiding this comment.
@tshimosake さん、 @SuperHotDogCat さん
ありがとうございます。if文確かに不要でした。
|
上でも言われているようにdefaultdictなのでif文はいらないかなと思います。それ以外は良いと思います。 |
|
因みにDPの解法(TLE)もあります。 |
There was a problem hiding this comment.
細かいですが、from collections import defaultdictから書いてあると、親切かなと思います。
(レビューの際、コードを実行して確認したりするケースもあるので)
There was a problem hiding this comment.
ありがとうございます、そこは考慮できていませんでした。できるだけ書く様にします。
There was a problem hiding this comment.
https://docs.python.org/3/library/collections.html#defaultdict-objects
https://docs.python.org/3.12/library/stdtypes.html#dict.get
.get(key, default=None) という手があります。
https://leetcode.com/problems/subarray-sum-equals-k/description/