Created
January 14, 2019 13:36
-
-
Save z0marlin/0ce063ac33b0d8628d563f4718791199 to your computer and use it in GitHub Desktop.
WEC Codebuddy Sem-4 Week-1 question-1 solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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