Skip to content

Instantly share code, notes, and snippets.

@z0marlin
Created January 14, 2019 13:36
Show Gist options
  • Save z0marlin/0ce063ac33b0d8628d563f4718791199 to your computer and use it in GitHub Desktop.
Save z0marlin/0ce063ac33b0d8628d563f4718791199 to your computer and use it in GitHub Desktop.
WEC Codebuddy Sem-4 Week-1 question-1 solution.
#define SUM(i) (i >= 0 ? sum[i] : 0)
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int i = 0, j = 0, len = nums.size(), min_len = INT_MAX;
if(len == 0)
return 0;
int sum[len];
sum[0] = nums[i];
for(i = 1; i < len; i++)
sum[i] = sum[i-1] + nums[i];
i = 0;
while(j <= i && i < len){
// printf("%d %d\n", SUM(i), SUM(j));
if(SUM(i) - SUM(j-1) >= s){
min_len = min(min_len, i - j + 1);
j++;
}
else
i++;
}
if(min_len == INT_MAX)
return 0;
return min_len;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment