-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path927.三等分.js
79 lines (72 loc) · 1.9 KB
/
927.三等分.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* @lc app=leetcode.cn id=927 lang=javascript
*
* [927] 三等分
*/
// @lc code=start
/**
* @param {number[]} A
* @return {number[]}
*/
var threeEqualParts = function(A) {
const getOneCount = () => {
const oneCount = A.reduce((acc, bit) => bit == 1 ? acc + 1 : acc, 0)
if(oneCount % 3 == 0)
return oneCount / 3
return -1
}
const getTail = oneCount => {
let zeroCount = 0, idx = A.length -1
let oneRemain = oneCount
while(oneRemain > 0) {
if(A[idx] == 1) {
oneRemain--
}
if(A[idx] == 0 && oneRemain == oneCount) {
zeroCount++
}
idx--
}
return {zeroCount, tailStart: idx + 1}
}
const getHead = (oneCount, zeroCount) => {
let idx = 0
while(oneCount > 0) {
if(A[idx] == 1)
oneCount--
idx++
}
if(zeroCount > 0) {
while(A[idx] == 0 && zeroCount > 0){
idx++
zeroCount--
}
}
return zeroCount == 0 ? idx-1 : -1
}
const getMiddle = (oneCount, headEnd, tailStart) => {
let idx = headEnd + 1
let tailIdx = tailStart
while(A[idx] != 1) idx++
while(idx < tailStart && A[idx] == A[tailIdx]) {
idx++
tailIdx++
}
if(tailIdx != A.length)
return -1
return idx
}
const oneCount = getOneCount()
if(oneCount == -1 || A.length < 3)
return [-1, -1]
if(oneCount == 0) return [0, A.length-1]
const {tailStart, zeroCount} = getTail(oneCount)
const headEnd = getHead(oneCount, zeroCount)
if(headEnd == -1)
return [-1, -1]
const middleEnd = getMiddle(oneCount, headEnd, tailStart)
if(middleEnd == -1)
return [-1, -1]
return [headEnd, middleEnd]
};
// @lc code=end