You are given a binary array, nums, consisting of only 0s and 1s. Your task is to determine the maximum number of consecutive 1s present in this array.
Details:
-
Input: A binary array
numswhere1 <= nums.length <= 100,000. Each element in the array is either0or1. -
Output: An integer representing the longest sequence of consecutive
1s in the given array.
Input: nums = [1,1,0,1,1,1]
Output: 3
Input: nums = [1,0,1,1,0,1]
Output: 2
function maxConsecutiveOnesSimple(nums) {
let maxCount = 0;
let currentCount = 0;
for (const num of nums) {
if (num === 1) {
currentCount++;
maxCount = Math.max(maxCount, currentCount);
} else {
currentCount = 0;
}
}
return maxCount;
}function maxConsecutiveOnesSlidingWindow(nums) {
let left = 0;
let right = 0;
let maxCount = 0;
while (right < nums.length) {
if (nums[right] === 1) {
right++;
} else {
maxCount = Math.max(maxCount, right - left);
right++;
left = right;
}
}
maxCount = Math.max(maxCount, right - left);
return maxCount;
}function maxConsecutiveOnesTwoPointers(nums) {
let maxCount = 0;
let left = 0;
for (let right = 0; right < nums.length; right++) {
if (nums[right] === 0) {
left = right + 1;
}
maxCount = Math.max(maxCount, right - left + 1);
}
return maxCount;
}function maxConsecutiveOnesReduce(nums) {
let maxCount = 0;
let currentCount = 0;
nums.reduce((_, num) => {
if (num === 1) {
currentCount++;
} else {
maxCount = Math.max(maxCount, currentCount);
currentCount = 0;
}
maxCount = Math.max(maxCount, currentCount);
}, 0);
return maxCount;
}function maxConsecutiveOnesRegex(nums) {
const binaryString = nums.join('');
const match = binaryString.match(/1+/g) || [];
const maxCount = Math.max(...match.map(seq => seq.length));
return maxCount;
}function maxConsecutiveOnesBinarySearch(nums) {
const countOnes = (arr) => {
let maxCount = 0;
let currentCount = 0;
for (const num of arr) {
if (num === 1) {
currentCount++;
maxCount = Math.max(maxCount, currentCount);
} else {
currentCount = 0;
}
}
return maxCount;
};
let maxCount = countOnes(nums);
return maxCount;
}function maxConsecutiveOnesDP(nums) {
if (nums.length === 0) return 0;
let dp = new Array(nums.length).fill(0);
dp[0] = nums[0];
let maxCount = dp[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] === 1) {
dp[i] = dp[i - 1] + 1;
} else {
dp[i] = 0;
}
maxCount = Math.max(maxCount, dp[i]);
}
return maxCount;
}function maxConsecutiveOnesStack(nums) {
let stack = [];
let maxCount = 0;
for (const num of nums) {
if (num === 1) {
stack.push(1);
} else {
if (stack.length > 0) {
maxCount = Math.max(maxCount, stack.length);
stack = [];
}
}
}
maxCount = Math.max(maxCount, stack.length);
return maxCount;
}function maxConsecutiveOnesMap(nums) {
const counts = new Map();
let currentCount = 0;
let maxCount = 0;
for (const num of nums) {
if (num === 1) {
currentCount++;
} else {
counts.set(currentCount, (counts.get(currentCount) || 0) + 1);
maxCount = Math.max(maxCount, currentCount);
currentCount = 0;
}
}
counts.set(currentCount, (counts.get(currentCount) || 0) + 1);
maxCount = Math.max(maxCount, currentCount);
return maxCount;
}function maxConsecutiveOnesSet(nums) {
let maxCount = 0;
let currentCount = 0;
for (const num of nums) {
if (num === 1) {
currentCount++;
maxCount = Math.max(maxCount, currentCount);
} else {
currentCount = 0;
}
}
return maxCount;
}