LoginSignup
0
0

More than 1 year has passed since last update.

Leetcode 209. Minimum Size Subarray Sum

Posted at

209. Minimum Size Subarray Sum

アプローチ

Sliding window(?) Two Pointer

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int start = 0;
        int end = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;

        while (start < nums.length) {
            sum += nums[start++]; // 合算
            while (target <= sum) { // 基準を超過した場合、範囲を小さくする
                result = Math.min(start - end, result);
                sum -= nums[end++]; // マイナス計算
            }
        }

        return result == Integer.MAX_VALUE ? 0 : result;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0