underwindfall / Algorithme

练习总结算法的地方
https://qifanyang.com/resume
1 stars 0 forks source link

LongestOnes1004 #400

Open underwindfall opened 2 years ago

underwindfall commented 2 years ago
public int longestOnes(int[] nums, int k) {
        // 1 -> 0
        int ans = 0;
        for (int left = 0, right = 0, count = 0; right < nums.length; right++) {
            count += nums[right] == 0 ? 1 : 0;
            while (count > k) {
                count -= nums[left] == 0 ? 1 : 0;
                left++;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }