题目
解法:prefix+hashmap+greedy
参考leetcode给的两条hint:
- Keep track of prefix sums to quickly look up what subarray that sums “target” can be formed at each step of scanning the input array.
- It can be proved that greedily forming valid subarrays as soon as one is found is optimal.
详细解释见代码注释
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
# dict store the previously appeared prefix sum. Reset it if we find a valid subarray. Note that we need to store the prefix sum so that we don't need to do sum over and over again
# why using dict. Because at a n