487. Max Consecutive Ones II
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the maximum number of consecutive 1s in a binary array, with the option to flip at most one 0 to a 1. This can be rephrased as finding the longest contiguous subarray that contains at most one 0. The length of this subarray will be our answer.
Solution
Max Consecutive Ones II in Python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
max_length = 0
left = 0
zero_count = 0
# 'right' is the right pointer of the sliding window.
for right in range(len(nums)):
# If the current element is 0, increment the zero counter.
if nums[right] == 0:
zero_count += 1
# While the number of zeros in the current window [left, right] is more than 1,
# we need to shrink the window from the left.
while zero_count > 1:
# If the element at the left pointer is a 0, decrement zero_count.
if nums[left] == 0:
zero_count -= 1
# Move the left pointer to the right to shrink the window.
left += 1
# The current window [left, right] is valid (has at most one 0).
# Calculate its length and update the maximum length found so far.
max_length = max(max_length, right - left + 1)
return max_length
Max Consecutive Ones II in Java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int maxLength = 0;
int left = 0;
int zeroCount = 0;
// 'right' is the right pointer of the sliding window.
for (int right = 0; right < nums.length; right++) {
// If the current element is 0, increment the zero counter.
if (nums[right] == 0) {
zeroCount++;
}
// While the number of zeros in the current window [left, right] is more than 1,
// we need to shrink the window from the left.
while (zeroCount > 1) {
// If the element at the left pointer is a 0, decrement zeroCount.
if (nums[left] == 0) {
zeroCount--;
}
// Move the left pointer to the right to shrink the window.
left++;
}
// The current window [left, right] is valid (has at most one 0).
// Calculate its length and update the maximum length found so far.
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}
Max Consecutive Ones II in C++
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int maxLength = 0;
int left = 0;
int zeroCount = 0;
// 'right' is the right pointer of the sliding window.
for (int right = 0; right < nums.size(); ++right) {
// If the current element is 0, increment the zero counter.
if (nums[right] == 0) {
zeroCount++;
}
// While the number of zeros in the current window [left, right] is more than 1,
// we need to shrink the window from the left.
while (zeroCount > 1) {
// If the element at the left pointer is a 0, decrement zeroCount.
if (nums[left] == 0) {
zeroCount--;
}
// Move the left pointer to the right to shrink the window.
left++;
}
// The current window [left, right] is valid (has at most one 0).
// Calculate its length and update the maximum length found so far.
maxLength = max(maxLength, right - left + 1);
}
return maxLength;
}
};
Max Consecutive Ones II in TypeScript
function findMaxConsecutiveOnes(nums: number[]): number {
let maxLength = 0;
let left = 0;
let zeroCount = 0;
// 'right' is the right pointer of the sliding window.
for (let right = 0; right < nums.length; right++) {
// If the current element is 0, increment the zero counter.
if (nums[right] === 0) {
zeroCount++;
}
// While the number of zeros in the current window [left, right] is more than 1,
// we need to shrink the window from the left.
while (zeroCount > 1) {
// If the element at the left pointer is a 0, decrement zeroCount.
if (nums[left] === 0) {
zeroCount--;
}
// Move the left pointer to the right to shrink the window.
left++;
}
// The current window [left, right] is valid (has at most one 0).
// Calculate its length and update the maximum length found so far.
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.