#1546
Maximum Number of Non-Overlapping Subarrays With Sum Equals Target
specialist · 885 · lc medium +31 · verified · 48.8% accepted · 1,116 likes · top 35%
Description
Given an integer array nums and an integer target, find the maximum number of non-overlapping, non-empty contiguous subarrays that each have a sum exactly equal to target.
Example 1:
Input: nums = [1,1,1,1,1], target = 2
Output: 2
Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2).
Example 2:
Input: nums = [-1,3,5,1,4,2,-9], target = 6
Output: 2
Explanation: There are 3 subarrays with sum equal to 6.
([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.
Code
1
2
3