Skip to content

LongestOnes1004 #400

@underwindfall

Description

@underwindfall
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;
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions