209.Minimum Size Subarray Sum

方法1:用双指针滑动窗口。这是一种双指针应用类型题。复杂度O(n)。

class Solution {

public:
        int minSubArrayLen(int s, vector<int>& nums) {
                int len=nums.size();
                if(len==0) return 0;
                int left=0,right=0;
                int MinLength;
                int sum=0;
                for(int i=0;i<len;i++){
                        sum+=nums[i];
                        if(sum>=s) break;
                }
                if(sum>=s) MinLength=len;
                else return 0;
                sum=0;
                int new_length=0;
                while(left<len){
                        while(right<len && sum<s){
                                sum+=nums[right];
                                right++;
                        }
                        new_length=right-left;
                        if(sum>=s && MinLength>new_length){
                                MinLength=new_length;
                        }
                        sum-=nums[left];
                        left++;
                }
                return MinLength;
        }

};

方法2:二分法。时间复杂度O(N*logN)

思路:

网上说的一个思路:建立一个比原数组长一位的sums数组,其中sums[i]表示nums数组中[0, i - 1]的和,然后我们对于sums中每一个值sums[i],用二分查找法找到子数组的右边界位置,使该子数组之和大于sums[i] + s,然后我们更新最短长度的距离即可。

自己当时用的唯一不同是sums数组并没有比原来长一位,sums[i]表示sums数组中[0,i]的和。

class Solution {
public:
        int minSubArrayLen(int s, vector<int>& nums) {
                int len=nums.size();
                if(len==0) return 0;
                int MinLength;
                int sum=0;
                //如果所有的值加起来都小于s,就没有继续下去的必要了
                for(int i=0;i<len;i++){
                        sum+=nums[i];
                        if(sum>=s) break;
                }
                if(sum>=s){
                        MinLength=len;
                }else{
                        return 0;
                }


                //前面所有元素累加的数组
                int* all=new int[len];
                all[0]=nums[0];
                for(int i=1;i<len;i++){
                        all[i]=all[i-1]+nums[i];
                }
                //以第一个元素作为左边界,确定大于s的最小右边界
                int left=0,right=len-1;
                int mid;
                if(all[right]<s){
                        delete []all;
                        return 0;
                }
                while(left<right){
                        mid=(left+right)/2;
                        if(all[mid]<s){
                                left=mid+1;
                        }else{
                                right=mid;
                        }
                }
                if(MinLength>right-0+1){
                        MinLength=right-0+1;
                }
                //以第当前元素作为左边界,确定大于s的最小右边界
                for(int i=1;i<len;i++){
                        left=i,right=len-1;
                        if(all[right]-all[left-1]<s) continue;
                        while(left<right){
                                mid=(left+right)/2;
                                if(all[mid]-all[i-1]<s){
                                        left=mid+1;
                                }else{
                                        right=mid;
                                }
                        }
                        if(MinLength>right-i+1){
                                MinLength=right-i+1;
                        }
                }
                delete []all;
                return MinLength;
        }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This problem can be solved using binary search. We can try to find the minimum possible maximum sum in a subarray by binary searching over the range of possible values. The upper bound of this range is the sum of all elements in the array, since each subarray must have at least one element. The lower bound of this range is the maximum element in the array, since each subarray must have at least one element and the maximum element must be in its own subarray. For each guess of the maximum sum, we can try to divide the array into subarrays such that no subarray has a sum greater than the guess. This can be done by iterating through the array and greedily assigning each element to the current subarray until the sum of the subarray is greater than the guess. Then, we start a new subarray with the current element. If we can divide the array into k subarrays with a maximum sum no greater than our guess, we can try a smaller guess. If we cannot divide the array into k subarrays with a maximum sum no greater than our guess, we need to try a larger guess. Here's some sample code in Python: ``` n, k = map(int, input().split()) arr = list(map(int, input().split())) low = max(arr) high = sum(arr) while low < high: mid = (low + high) // 2 count = 1 total = 0 for x in arr: if total + x > mid: count += 1 total = x else: total += x if count > k: low = mid + 1 else: high = mid print(low) ``` This code reads in the input and initializes the range of possible values for the maximum sum in a subarray. Then, it performs binary search to find the minimum possible maximum sum. For each guess of the maximum sum, it tries to divide the array into k subarrays such that no subarray has a sum greater than the guess. If it can do this, it tries a smaller guess. If not, it tries a larger guess. Finally, it prints out the minimum possible maximum sum.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值