Skip to content

Commit

Permalink
Time: 270 ms (34.62%), Space: 84.1 MB (79.62%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Mar 13, 2023
1 parent a0094bb commit e9b37bf
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int maxNonOverlapping(vector<int>& nums, int target) {
unordered_map<int, int> dp;
dp[0] = -1;
int sum = 0, right = -1, cnt = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
if (dp.count(sum - target)) {
int left = dp[sum - target];
if (right <= left) {
cout<<i<<" ";
++cnt;
right = i;
}
}
dp[sum] = i;
}
return cnt;
}
};

0 comments on commit e9b37bf

Please sign in to comment.