Advertisement
sweet1cris

Untitled

Jan 9th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class Solution {
  2.     /**
  3.      * @param nums: an array of integers
  4.      * @param s: an integer
  5.      * @return: an integer representing the minimum size of subarray
  6.      */
  7.     public int minimumSize(int[] nums, int s) {
  8.         // write your code here
  9.         int j = 0, i = 0;
  10.         int sum =0;
  11.         int ans = Integer.MAX_VALUE;
  12.         for(i = 0; i < nums.length; i++) {
  13.             while(j < nums.length && sum < s) {
  14.                 sum += nums[j];
  15.                 j ++;
  16.             }
  17.             if(sum >=s) {
  18.                 ans = Math.min(ans, j - i);
  19.             }
  20.             sum -= nums[i];
  21.         }
  22.         if(ans == Integer.MAX_VALUE)
  23.             ans = -1;
  24.         return ans;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement