Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /*
  2. * 209. Minimum Size Subarray Sum
  3. * Given an array of n positive integers and a positive integer s,
  4. * find the minimal length of a contiguous subarray of which the
  5. * sum ≥ s. If there isn't one, return 0 instead.
  6. *
  7. * Example:
  8. * Input: s = 7, nums = [2,3,1,2,4,3]
  9. * Output: 2
  10. * Explanation: the subarray [4,3] has the minimal length under the problem constraint.
  11. */
  12. #include <stdio.h>
  13. #include <stdbool.h>
  14.  
  15. bool min_array(int target, int *nums, int numsSize, int *curr_result) {
  16. (*curr_result)++;
  17. bool flag = false;
  18. if (*nums==target) flag = true;
  19. else {
  20. if (*nums>target) flag = false;
  21. else {
  22. if (numsSize>1) {
  23. flag = min_array(target-*nums, nums+1,
  24. numsSize-1, curr_result);
  25. }
  26.  
  27. }
  28. }
  29. return flag;
  30. }
  31.  
  32. int minSubArrayLen(int s, int *nums, int numsSize){
  33. int curr_result = 0;
  34. int result = numsSize+1;
  35. for(int i=0; i<numsSize, result!=1; ++i) {
  36. curr_result = 0;
  37. if (min_array(s, nums+i, numsSize, &curr_result)
  38. && (curr_result<result)) {
  39. result=curr_result;
  40. }
  41. }
  42. return result;
  43. }
  44.  
  45.  
  46. int main() {
  47. int v[] = {2,3,1,2,4,3};
  48. printf("%dn", minSubArrayLen(3, v, 6));
  49.  
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement