-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path849-maximize-distance-to-closest-person.swift
47 lines (41 loc) · 1.4 KB
/
849-maximize-distance-to-closest-person.swift
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
class Solution {
func distance(from: Int, to: Int) -> Int {
if to < from { return distance(from: to, to: from) }
return to - from
}
func maxDistToClosest(_ seats: [Int]) -> Int {
// Eliminate left free seats
var firsOccupiedSeat = Int.min
for seat in 0..<seats.count {
if seats[seat] == 1 {
firsOccupiedSeat = seat
break
}
}
// Eliminate right free seats
var lastOccupiedSeat = Int.max
for index in 0..<seats.count { // iterate from end to start
let seat = seats.count - 1 - index
if seats[seat] == 1 {
lastOccupiedSeat = seat
break
}
}
let firstFreeSeatsDistance = distance(from: 0, to: firsOccupiedSeat)
let lastFreeSeatsDistance = distance(from: lastOccupiedSeat, to: seats.count - 1)
var maxDistance = max(1, max(firstFreeSeatsDistance, lastFreeSeatsDistance))
guard firsOccupiedSeat != lastOccupiedSeat else { return maxDistance }
var leftNeighbor = firsOccupiedSeat
for seat in firsOccupiedSeat...lastOccupiedSeat {
if seats[seat] == 1 {
let half = (seat - leftNeighbor) / 2
let mid = leftNeighbor + half
let leftDistance = distance(from: leftNeighbor, to: mid)
let rightDistance = distance(from: mid, to: seat)
maxDistance = max(maxDistance, min(leftDistance, rightDistance))
leftNeighbor = seat
}
}
return maxDistance
}
}