Advertisement
sweet1cris

Untitled

Jan 9th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public class Solution {
  2.     /**
  3.      * @param nums: a list of integers
  4.      * @return: A integer indicate the sum of minimum subarray
  5.      */
  6.     public int minSubArray(ArrayList<Integer> nums) {
  7.         if (nums == null)
  8.             return 0;
  9.         int len = nums.size();
  10.         int min = Integer.MAX_VALUE, currSum = 0;
  11.         int []localmin  = new int[len];
  12.         int []globalmin = new int[len];
  13.         for (int i = 0; i < len; i++) {
  14.             if( i ==0 )
  15.                 globalmin[i] = localmin[i] = nums.get(i);
  16.             else {
  17.                 localmin[i] = Math.min(localmin[i - 1] + nums.get(i), nums.get(i));
  18.                 globalmin[i] = Math.min(globalmin[i - 1], localmin[i]);
  19.             }
  20.            
  21.         }
  22.         return globalmin[len-1];
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement