mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
problems: add maximize distance to closest
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
16763cdd2f
commit
2c664a305d
1 changed files with 18 additions and 0 deletions
18
problems/maximize-distance-to-closest-person.kt
Normal file
18
problems/maximize-distance-to-closest-person.kt
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
class Solution {
|
||||||
|
private fun mapSeats(seats: IntArray): MutableList<Int> =
|
||||||
|
seats.map { if (it == 1) 0 else Int.MAX_VALUE }.toMutableList()
|
||||||
|
|
||||||
|
fun maxDistToClosest(seats: IntArray): Int {
|
||||||
|
val left: MutableList<Int> = mapSeats(seats)
|
||||||
|
for (i in left.indices.drop(1).filter { left[it] != 0 }) {
|
||||||
|
left[i] = left[i - 1] + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val right: MutableList<Int> = mapSeats(seats)
|
||||||
|
for (i in right.indices.reversed().drop(1).filter { right[it] != 0 }) {
|
||||||
|
right[i] = right[i + 1] + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return left.zip(right).map { (l, r) -> if (l < r) l else r }.max()!!
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue