-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ120.js
41 lines (38 loc) · 801 Bytes
/
Q120.js
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
/**
* @param {number[]} nums
* @return {number}
*/
let subArray = (a) => {
let arr = [];
for (let i = 0; i < a.length - 1; i++) {
for (let j = i + 1; j < a.length + 1; j++) {
let temp = a.slice(i, j);
arr.push(temp)
}
}
return arr;
};
var findMaxLength = function (nums) {
if (nums.length === 1) {
return 0;
}
if (
nums.length === 2 &&
((nums[0] === 0 && nums[1] === 1) || (nums[1] === 0 && nums[0] === 1))
) {
return 2;
}
let all = subArray(nums);
all.sort((a, b) => b.length - a.length);
for (let i = 0; i < all.length; i++) {
all[i].sort((a, b) => {
return a - b;
});
if (all[i][all[i].length / 2 ] === 1) {
return all[i].length;
}
}
return 0;
};
let nums = [0, 1];
console.log(findMaxLength(nums));