zoukankan      html  css  js  c++  java
  • leetcode@ [209]Minimum Size Subarray Sum

    https://leetcode.com/problems/minimum-size-subarray-sum/

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.

    For example, given the array [2,3,1,2,4,3] and s = 7,
    the subarray [4,3] has the minimal length under the problem constraint.

    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            vector<int> vec = nums;
            
            for(int i=1;i<nums.size();++i){
                nums[i]+=nums[i-1];
            }
            
            int minLen = numeric_limits<int>::max();
            for(int i=0;i<nums.size();++i){
                int low = i, high = nums.size()-1, mid;
                while(low < high){
                    mid = (low+high)/2;
                    if(nums[mid]-nums[i]+vec[i] < s)  low = mid+1;
                    else high = mid;
                }
                if(nums[low]-nums[i]+vec[i]>=s && low-i+1<minLen) minLen = low-i+1;
            }
            
            if(minLen == numeric_limits<int>::max()) return 0;
            else return minLen;
        }
    };
  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    thinkphp使用foreach遍历的方法
    php中foreach中使用&的办法
    thinkphp做搜索功能
    数据库虚拟主机目录的配置文件
    网页响应式设计原理
    数据库常见远程连接问题
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4927820.html
Copyright © 2011-2022 走看看