-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
44 lines (42 loc) · 1.25 KB
/
Copy pathindex.ts
File metadata and controls
44 lines (42 loc) · 1.25 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* # 485. Max Consecutive Ones
* @link [LeetCode](https://leetcode.com/problems/max-consecutive-ones/description/)
*
* @description
* Given a binary array nums, return the maximum number of consecutive 1's in the array.
* Constraints:
* - 1 <= nums.length <= 105
* - nums[i] is either 0 or 1.
*
* @example findMaxConsecutiveOnes([1,1,0,1,1,1]): 3
* Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
*
* @example findMaxConsecutiveOnes([1,0,1,1,0,1]): 2
*
* @function findMaxConsecutiveOnes
* @param {number[]} nums
* @returns number
*/
function findMaxConsecutiveOnes(nums: number[]): number {
let savedPoint = 0;
let counter = 0;
for (let i = 0; i < nums.length; i++) {
if (nums.at(i) === 1) {
counter++;
continue;
}
savedPoint = Math.max(savedPoint, counter);
counter = 0;
}
return Math.max(savedPoint, counter);
}
function findMaxConsecutiveOnes2(nums: number[]): number {
let savedPoint = 0;
let counter = 0;
for (let i = 0; i < nums.length; i++) {
counter = nums.at(i) === 0 ? 0 : counter + 1;
savedPoint = Math.max(savedPoint, counter);
}
return savedPoint;
}
export { findMaxConsecutiveOnes, findMaxConsecutiveOnes2 };