diff --git a/problems/maximize-distance-to-closest-person.kt b/problems/maximize-distance-to-closest-person.kt new file mode 100644 index 0000000..92ef3b6 --- /dev/null +++ b/problems/maximize-distance-to-closest-person.kt @@ -0,0 +1,18 @@ +class Solution { + private fun mapSeats(seats: IntArray): MutableList = + seats.map { if (it == 1) 0 else Int.MAX_VALUE }.toMutableList() + + fun maxDistToClosest(seats: IntArray): Int { + val left: MutableList = mapSeats(seats) + for (i in left.indices.drop(1).filter { left[it] != 0 }) { + left[i] = left[i - 1] + 1 + } + + val right: MutableList = 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()!! + } +}