lintcode 44.最小子数组

给定一个整数数组,找到一个具有最小和的子数组,保证子数组最少包含一个数字,返回其最小和。

例如[1,-1,-2,1]返回-3

思路:与最大子数组一样的思路。客观存在的最小子数组的任意前缀都不可能是正的,否则就会有一个更小的子数组。

public int minSubArray(List<Integer> nums) {
	        int min=Integer.MAX_VALUE;
	        int current=0;
	        for(int i=0;i<nums.size();i++)
	        {
	            current+=nums.get(i);
	            min=Math.min(min, current);
	            if(current>=0)
	            current=0;
	        }
	        return min;
}

猜你喜欢

转载自blog.csdn.net/thyzy120605/article/details/80668947