Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 209. Minimum Size Subarray Sum
- * Given an array of n positive integers and a positive integer s,
- * find the minimal length of a contiguous subarray of which the
- * sum ≥ s. If there isn't one, return 0 instead.
- *
- * Example:
- * Input: s = 7, nums = [2,3,1,2,4,3]
- * Output: 2
- * Explanation: the subarray [4,3] has the minimal length under the problem constraint.
- */
- #include <stdio.h>
- #include <stdbool.h>
- bool min_array(int target, int *nums, int numsSize, int *curr_result) {
- (*curr_result)++;
- bool flag = false;
- if (*nums==target) flag = true;
- else {
- if (*nums>target) flag = false;
- else {
- if (numsSize>1) {
- flag = min_array(target-*nums, nums+1,
- numsSize-1, curr_result);
- }
- }
- }
- return flag;
- }
- int minSubArrayLen(int s, int *nums, int numsSize){
- int curr_result = 0;
- int result = numsSize+1;
- for(int i=0; i<numsSize, result!=1; ++i) {
- curr_result = 0;
- if (min_array(s, nums+i, numsSize, &curr_result)
- && (curr_result<result)) {
- result=curr_result;
- }
- }
- return result;
- }
- int main() {
- int v[] = {2,3,1,2,4,3};
- printf("%dn", minSubArrayLen(3, v, 6));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement