forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxConsecutiveOnes.js
More file actions
Latest commit
30 lines (27 loc) · 700 Bytes
/
Copy pathMaxConsecutiveOnes.js
File metadata and controls
30 lines (27 loc) · 700 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @function maxConsecutiveOnes
* @description Given a binary array nums, return the maximum number of consecutive 1's in the array.
* @param {number[]} nums
* @return {number}
* @see [Leetcode link](https://leetcode.com/problems/max-consecutive-ones/)
*/
exportconstmaxConsecutiveOnes=(nums)=>{
if(!nums.length)return0
letresult=0
letk=0
for(
letslowPointer=0,fastPointer=0;
fastPointer<nums.length;
fastPointer++
){
if(nums[fastPointer]===0)k--
while(k<0){
if(nums[slowPointer]===0){
k++
}
slowPointer++
}
result=Math.max(result,fastPointer-slowPointer+1)
}
returnresult
}