-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
55 lines (48 loc) · 999 Bytes
/
solution1.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
/**
* https://leetcode.com/problems/maximize-distance-to-closest-person/
*
* 849. Maximize Distance to Closest Person
*
* Easy
*
* 60ms 91.69%
* 36mb 27.07%
*/
const maxDistToClosest = seats => {
const max = seats.length
let empty = 0
let maxDistance = 1
let startZero = 0
for (let i = 0; i < max; i++) {
if (seats[i] === 0) {
startZero++
continue
}
break
}
let endZero = 0
for (let i = max - 1; i >= 0; i--) {
if (seats[i] === 0) {
endZero++
continue
}
break
}
maxDistance = Math.max(maxDistance, startZero, endZero)
for (let i = 0; i < max; i++) {
const seat = seats[i]
if (seat === 1 && empty !== 0) {
if (empty % 2 === 0) {
maxDistance = Math.max(maxDistance, Math.floor(empty / 2))
} else {
maxDistance = Math.max(maxDistance, Math.floor(empty / 2) + 1)
}
empty = 0
continue
}
if (seat === 0) {
empty++
}
}
return maxDistance
}