-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
31 lines (28 loc) · 894 Bytes
/
solution.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
/**
* @param {number[]} seats
* @return {number}
*/
var maxDistToClosest = function(seats) {
let start = end = maxDistance = 0,
length = seats.length
seats.forEach((seat, i) => {
if (seat === 1) {
if (start === 0 || end === length - 1) {
maxDistance = Math.max(maxDistance, end - start + 1)
} else {
maxDistance = Math.max(maxDistance, parseInt((end - start + 1) / 2) + (end - start + 1) % 2)
}
if (i + 1 < length) {
start = end = i + 1
}
} else {
end = i
}
})
if (start === 0 || end === length - 1) {
maxDistance = Math.max(maxDistance, end - start + 1)
} else {
maxDistance = Math.max(maxDistance, parseInt((end - start + 1) / 2) + (end - start + 1) % 2)
}
return maxDistance
};