Given an array of integers, find the subarray with smallest sum.
Return the sum of the subarray.
Example
For [1, -1, -2, 1], return -3
题目和最大子串类似,解法也类似
public class Solution {
/**
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(ArrayList<Integer> nums) {
if (nums == null || nums.size() == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
int sum = 0;
for(int i = 0; i < nums.size(); i++) {
if(sum > 0) {
sum = 0;
}
sum += nums.get(i);
min = Math.min(sum, min);
}
return min;
}
}