-
Notifications
You must be signed in to change notification settings - Fork 10
MinimumSubarrayWithTarget #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f978904
3a73a73
b7b063a
51692fe
887e8fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int MinimumSubarrayWithTarget(vector<int>vect,int target,int minlen) { | ||
|
||
int windowStart = 0; | ||
int windowSum = 0; | ||
for(int windowEnd = 0; windowEnd < vect.size(); windowEnd++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call the array as arr or nums not vect. |
||
windowSum += vect[windowEnd]; | ||
while(windowSum>=target){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use proper space between the operands. |
||
int windowlength= windowStart-windowEnd+1; | ||
minlen=min(minlen,windowlength); | ||
windowSum -=vect[windowStart]; | ||
windowStart += 1; | ||
} | ||
} | ||
return minlen; | ||
} | ||
|
||
int main() { | ||
vector<int>vect={3,4,7,1,9,2,12,1}; | ||
int n=vect.size(); | ||
int target=14; | ||
int minlen=INT_MAX; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. user camelCasing like minLength |
||
int ans = MinimumSubarrayWithTarget(vect,target,minlen); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't pass anything in the function other than arr and target. Declare everything in the function itself and not in the main method. |
||
cout<<abs(ans); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class NoRepeatSubstring { | ||
public: | ||
static int noRepeatSubstring(string s) { | ||
int windowStart = 0; | ||
int maxLength = INT_MIN; | ||
map<char, int> indexMap; | ||
if(s.size() == 0) return 0; | ||
|
||
for(int windowEnd = 0; windowEnd < s.size(); windowEnd++) { | ||
char currChar = s[windowEnd]; | ||
if(indexMap.find(currChar) != indexMap.end()) { | ||
windowStart = max(windowStart, indexMap[currChar] + 1); | ||
} | ||
indexMap[currChar] = windowEnd; | ||
maxLength = max(maxLength, windowEnd - windowStart + 1); | ||
} | ||
return maxLength; | ||
} | ||
}; | ||
|
||
int main() { | ||
string s = "abcabcbb"; | ||
cout << NoRepeatSubstring::noRepeatSubstring(s) << endl; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use space after commas.