资讯专栏INFORMATION COLUMN

[LintCode] Minimum Size Subarray Sum

hyuan / 1132人阅读

摘要:做一个窗口,满足的左界到右界的距离最小值为所求。循环的约束条件要注意,不要遗漏不能超过的长度,但可以等于,因为存在所有元素之和为的极端情况。在时,先更新窗口为当前循环后的最小值,减去最左元素,指针后移。

Problem

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.

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.

Challenge

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(nlogn).

Note

做一个窗口minsize,满足sum >= s的左界到右界的距离最小值为所求。while循环的约束条件要注意,不要遗漏:right不能超过nums[]的长度,但可以等于,因为存在nums[]所有元素之和为s的极端情况。
sum < s时,sum加上右指针的元素,同时right指针后移,但是注意当right = nums.length时,sum不能加(元素不存在!),所以sum += nums[right]要加限制条件。
sum >= s时,先更新窗口为当前循环后的最小值,减去最左元素,left指针后移。

Solution
public class Solution {
    public int minimumSize(int[] nums, int s) {
        if (nums == null) return -1;
        int left = 0, right = 0, sum = 0, minsize = Integer.MAX_VALUE;
        while (right <= nums.length && left <= right) {
            if (sum < s) {
                if (right < nums.length) {
                    sum += nums[right];
                }
                right++;
            }
            else {
                minsize = Math.min(minsize, right - left);
                sum -= nums[left];
                left++;
            }
        }
        return minsize <= nums.length ? minsize : -1;
    }
}

Binary Search:

http://www.jyuan92.com/blog/leetcode-min...

public class Solution {
    public int minimumSize(int[] nums, int s) {
        if (nums == null || nums.length == 0) return -1;
        int[] sum = new int[nums.length+1];
        int minsize = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            sum[i+1] = sum[i] + nums[i];
            if (sum[i+1] >= s) {
                int left = helper(sum, sum[i+1]-s, 0, i+1);
                minsize = Math.min(minsize, i+1-left);
            }
        }
        return minsize > nums.length ? -1 : minsize;
    }
    public int helper(int[] A, int target, int start, int end) {
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (A[mid] < target) start = mid; 
            else end = mid; //A[mid] = target is in this branch
        }
        if (A[end] <= target) return end;//Make sure return the same brunch
                                         //---->end, when A[end] = target
        else return start;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65523.html

相关文章

  • [Leetcode] Minimum Size Subarray Sum 最短子串和

    摘要:双指针复杂度时间空间思路我们用两个指针维护一个窗口,保证这个窗口的内的和是小于目标数的。如果和仍小于目标数,则将窗口右边界右移。另外,如果左边界大于右边界时,说明最短子串的长度已经小于等于,我们就不用再查找了。 Minimum Size Subarray Sum Given an array of n positive integers and a positive integer ...

    wthee 评论0 收藏0
  • [LeetCode] 209. Minimum Size Subarray Sum (Easy ve

    Problem Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isnt one, return 0 instead. Example: Input: s =...

    HelKyle 评论0 收藏0
  • LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

    摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。 公众号: 爱写bug(ID:icodebugs)作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组...

    jas0n 评论0 收藏0
  • LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

    摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。 公众号: 爱写bug(ID:icodebugs)作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组...

    JayChen 评论0 收藏0
  • LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

    摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。 算法是一个程序的灵魂 公众号:爱写bug(ID:icodebugs)作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的...

    wow_worktile 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<