Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions MinimumSubarrayWithTarget.py
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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use space after commas.


int windowStart = 0;
int windowSum = 0;
for(int windowEnd = 0; windowEnd < vect.size(); windowEnd++) {
Copy link
Owner

Choose a reason for hiding this comment

The 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){
Copy link
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user camelCasing like minLength

int ans = MinimumSubarrayWithTarget(vect,target,minlen);
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}
27 changes: 27 additions & 0 deletions NoRepeatSubstring.cpp
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;
}