Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d70b18a

Browse files
Create 3364. Minimum Positive Sum Subarray .cpp
1 parent 9444d8e commit d70b18a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//TC : O(n*(r-l+1))
2+
//SC : O(1)
3+
4+
class Solution {
5+
public:
6+
int minimumSumSubarray(vector<int>& nums, int l, int r) {
7+
int ans = INT_MAX;
8+
for(int i=l;i<=r;i++){
9+
int temp = 0;
10+
for(int j=0;j<i;j++){
11+
temp+=nums[j];
12+
}
13+
if(min(ans,temp)>0){
14+
ans = min(ans,temp);
15+
}
16+
int k = 0;
17+
int a = i;
18+
while(a<nums.size()){
19+
temp+=nums[a];
20+
temp-=nums[k];
21+
if(min(ans,temp)>0){
22+
ans = min(ans,temp);
23+
}
24+
a++;
25+
k++;
26+
}
27+
}
28+
if(ans ==INT_MAX) return -1;
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /