头图

LeetCode-209 - Subarray with minimum length

雄狮虎豹
中文

the subarray with the smallest length

Topic description: Given an array of n positive integers and a positive integer target .

Find the smallest contiguous subarray [nums ~l~, nums~l+1~​​, ..., nums~r-1~, nums~r~] in the array that satisfies its sum ≥ target, and returns its length. Returns 0 if no matching subarray exists.

For example descriptions, please refer to the official website of LeetCode.

Source: LeetCode
Link: https://leetcode-cn.com/problems/minimum-size-subarray-sum/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Solution 1: Sliding window

First, judge the special case, if the array is empty, return 0 directly.

Otherwise, the sliding window method is used to determine whether there is the smallest continuous subarray. The specific processing logic is as follows:

  • First, declare the left and right boundaries of the sliding window, and use minLen to record the length of the smallest contiguous subarray;
  • Traverse the array until the right boundary traverses to the last number position of the original array;
  • Calculate the sum of the continuous subarrays of the current range, and judge whether the current sum is smaller than the target. If not, move the left border of the window to the right, and judge whether the current continuous length is the smallest; The right border is shifted to the right.

Finally, judge whether minLen has been found, and return 0 if it does not exist; otherwise, return minLen.

 public class LeetCode_209 {
    /**
     * 滑动窗口
     *
     * @param target 目标值
     * @param nums   原数组
     * @return
     */
    public static int minSubArrayLen(int target, int[] nums) {
        // 如果数组为空,直接返回0
        if (nums == null || nums.length == 0) {
            return 0;
        }
        // 记录最小的连续子数组的长度
        int minLen = Integer.MAX_VALUE;
        // 滑动窗口的左右边界
        int left = 0, right = 0;
        // 遍历直到右边界遍历到原数组的最后一个数字位置
        while (right < nums.length) {
            int sum = 0;
            // 计算当前范围的连续子数组的和
            for (int i = left; i <= right; i++) {
                sum += nums[i];
            }
            // 判断当前的和是否比target小,如果不小于,则将窗口的左边界右移,并且判断当前的连续长度是否是最小的;如果小于,则将窗口的右边界右移
            if (sum >= target) {
                minLen = Math.min(minLen, right - left + 1);
                left++;
            } else {
                right++;
            }
        }

        if (minLen == Integer.MAX_VALUE) {
            return 0;
        }
        return minLen;
    }

    public static void main(String[] args) {
        // 测试用例一,期望输出: 2
        System.out.println(minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}));
        // 测试用例二,期望输出: 0
        System.out.println(minSubArrayLen(11, new int[]{1, 1, 1, 1, 1, 1, 1, 1}));
    }
}
[Daily Message] Life is like climbing a mountain, but finding a way to a mountain is a learning process. During this process, we should learn to be firm and calm, and learn how to find vitality from panic.
阅读 468

LeetCodet题解
技术学习

玉树临风,仙姿佚貌!

1.7k 声望
7.1k 粉丝
0 条评论

玉树临风,仙姿佚貌!

1.7k 声望
7.1k 粉丝
文章目录
宣传栏