Leetcode Problem 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target
1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target
Leetcode Solutions
Prefix Sum with Hash Map
Initialize a hash map to store the cumulative sum and its corresponding index, and set the initial sum to 0 with index -1.
Initialize variables for the cumulative sum, the count of non-overlapping subarrays, and the last index of the previous subarray found.
Iterate through the array, updating the cumulative sum at each step.
Check if the cumulative sum minus the target is in the hash map.
If it is, and the last index of the previous subarray is less than or equal to the index stored in the hash map, increment the count and update the last index to the current index.
Update the hash map with the current cumulative sum and index.