-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path485_Max_Consecutive_Ones.js
More file actions
64 lines (54 loc) · 1.4 KB
/
485_Max_Consecutive_Ones.js
File metadata and controls
64 lines (54 loc) · 1.4 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
*/
const expect = require('expect');
describe('485 Max Consecutive Ones', () => {
it('returns a complement number of 3', () => {
//arrange
const input = [1, 1, 0, 1, 1, 1];
const expected = 3;
//act
const actual = findMaxConsecutiveOnes_1(input);
//assert
expect(actual).toBe(expected);
});
it('returns a complement number of 4', () => {
//arrange
const input = [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1];
const expected = 4;
//act
const actual = findMaxConsecutiveOnes_1(input);
//assert
expect(actual).toBe(expected);
});
it('returns 0', () => {
//arrange
const input = [0];
const expected = 0;
//act
const actual = findMaxConsecutiveOnes_1(input);
//assert
expect(actual).toBe(expected);
});
});
const findMaxConsecutiveOnes = nums => Math.max(...nums
.join('')
.split('0')
.map(value => value.length));
const findMaxConsecutiveOnes_1 = (nums) => {
let result = 0;
let counter = 0;
for (let index = 0; index < nums.length; index++) {
if (nums[index] === 1) {
counter += 1;
result = Math.max(result, counter);
} else {
counter = 0;
}
}
return result;
};