Eclipse OA 面试真题解析:最大化停车位之间的最小距离

29次阅读
没有评论

You are given an array Parkings of size N, which represents parking spaces, and an integer K, which represents the number of cars you need to park in Parkings. The array Parkings contains only 0 and 1, where 0 represents an empty slot and 1 represents an occupied slot.

You have to park the cars in such a way that the minimum distance between cars is maximized.

Return the maximum distance that you can achieve.

这道题本质上是“最大化最小值”的安排问题:给定一排停车位,其中 1 表示已有车辆、0 表示空位,需要再停车 K 辆,并让任意两辆车之间的最小距离尽可能大。常见做法是结合贪心与二分答案:先把“距离 d 是否可行”作为判定问题,再通过线性扫描检查能否在满足间距 d 的前提下放下 K 辆车。由于停车位只包含 0/1,检查过程可以用一次遍历完成,整体适合处理较大的数组。

正文完
 0