lintcode 中等题:Minimum Size Subarray 和大于S的最小子数组

题目

和大于S的最小子数组

给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组。如果无解,则返回 -1。

样例

给定数组 [2,3,1,2,4,3] 和 s = 7, 子数组 [4,3] 是该条件下的最小长度子数组。

挑战

如果你已经完成了O(n)时间复杂度的编程,请再试试 O(n log n)时间复杂度。

解题

定义两个指针,slow,fast,以先后速度向右走

fast先找到第一个是的sum>s的值

根据fast和slow计算当前子数组的长度

sum-=nums[slow],寻找最后一个不满足sum>=s 的slow,每次更新子数组长度的最短值。

这里一般会问什么没有对数组排序的问题,这里的数组都是正数,如果是负数会出现问题,这里怎么加,sum值都是增加的,顺序至少影响增加的速度的问题,同时我们向前走到了sum>=s的值后还要对后面的slow的部分进行检查。这样解释的不是很充分,具体我也不知道了。

还要网上说时间复杂度是O(N),我怎么感觉时间复杂度应该很接近O(N2),不知道为什么?

操作如下图所示

public class Solution {
    /**
     * @param nums: an array of integers
     * @param s: an integer
     * @return: an integer representing the minimum size of subarray
     */
    public int minimumSize(int[] nums, int s) {
        // write your code here
        if(nums.length == 0 || nums == null)
            return -1;
        int slow = 0;
        int fast = 0;
        int res = Integer.MAX_VALUE;
        int sum = 0;
        while(fast < nums.length){
            while( fast < nums.length && sum < s){
                sum += nums[fast];
                fast += 1;
                // System.out.println(sum+" "+fast);
            }
            // System.out.println();
            while(sum>=s){
                res = Math.min(res,fast - slow);
                sum -=nums[slow];
                slow +=1;
            }
        }
        return res==Integer.MAX_VALUE?-1:res;
    }
}

Java Code

总耗时: 1910 ms

class Solution:
     # @param nums: a list of integers
     # @param s: an integer
     # @return: an integer representing the minimum size of subarray
    def minimumSize(self, nums, s):
        # write your code here
        if s == None or len(nums) == 0:
            return -1;
        lens = len(nums)
        slow = 0
        fast = 0
        sum = 0
        res = lens+1
        while fast < lens:
            while fast < lens and sum < s:
                sum += nums[fast]
                fast +=1
            while slow < fast and sum>= s:
                res = min(res,fast - slow)
                sum -= nums[slow]
                slow +=1
        if res ==lens+1:
            return -1
        else:
            return res

Python Code

总耗时: 444 ms

时间: 2024-08-07 00:16:37

lintcode 中等题:Minimum Size Subarray 和大于S的最小子数组的相关文章

leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution 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, r

lintcode 中等题:continuous subarray sum 联系子数组之和

题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, 1, 3, -3, 4], 返回[1,4]. 解题 法一:直接暴力,时间复杂度O(N2),时间超时 public class Solution { /** * @param A an integer array * @return A list of integers includes the i

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

[LeetCode] 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

【LeetCode】209. Minimum Size Subarray Sum

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 subar

LintCode-和大于S的最小子数组

给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 您在真实的面试中是否遇到过这个题? Yes 样例 给定数组 [2,3,1,2,4,3] 和 s = 7, 子数组 [4,3] 是该条件下的最小长度子数组. 挑战 如果你已经完成了O(n)时间复杂度的编程,请再试试 O(n log n)时间复杂度. 标签 Expand 相关题目 Expand 分析:数组的题目一般都是前缀和阿(对应O(nlgn)的解法),或者就是两根指针同

[LintCode] 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 -1 instead. Have you met this question in a real interview? Example Given the array [2,3,1,2,4,3] a

[刷题] LeetCode 209 Minimum Size Subarray Sum

要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的连续子数组 思路 暴力解法(n3) 滑动窗口() 1 class Solution{ 2 public: 3 int minSubArrayLen(int s, vector<int>& nums){ 4 int