在代号为 C-137 的地球上,Rick 发现如果他将两个球放在他新发明的篮子里,它们之间会形成特殊形式的磁力。Rick 有 n
个空的篮子,第 i
个篮子的位置在 position[i]
,Morty 想把 m
个球放到这些篮子里,使得任意两球间 最小磁力 最大。
已知两个球如果分别位于 x
和 y
,那么它们之间的磁力为 |x - y|
。
给你一个整数数组 position
和一个整数 m
,请你返回最大化的最小磁力。
示例 1:
输入:position = [1,2,3,4,7], m = 3 输出:3 解释:将 3 个球分别放入位于 1,4 和 7 的三个篮子,两球间的磁力分别为 [3, 3, 6]。最小磁力为 3 。我们没办法让最小磁力大于 3 。
示例 2:
输入:position = [5,4,3,2,1,1000000000], m = 2 输出:999999999 解释:我们使用位于 1 和 1000000000 的篮子时最小磁力最大。
提示:
n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
- 所有
position
中的整数 互不相同 。 2 <= m <= position.length
先对 position 进行排序。
然后二分枚举磁力值(相邻两球的最小间距),统计当前最小磁力值下能放下多少个小球,记为 cnt。若 cnt >= m
,说明此磁力值符合条件。继续二分查找,最终找到符合条件的最大磁力值。
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(f):
prev = position[0]
cnt = 1
for curr in position[1:]:
if curr - prev >= f:
prev = curr
cnt += 1
return cnt >= m
position.sort()
left, right = 1, position[-1]
while left < right:
mid = (left + right + 1) >> 1
if check(mid):
left = mid
else:
right = mid - 1
return left
class Solution {
public int maxDistance(int[] position, int m) {
Arrays.sort(position);
int left = 1, right = position[position.length - 1];
while (left < right) {
int mid = (left + right + 1) >>> 1;
if (check(position, mid, m)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
private boolean check(int[] position, int f, int m) {
int prev = position[0];
int cnt = 1;
for (int i = 1; i < position.length; ++i) {
int curr = position[i];
if (curr - prev >= f) {
prev = curr;
++cnt;
}
}
return cnt >= m;
}
}
class Solution {
public:
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
int left = 1, right = position[position.size() - 1];
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check(position, mid, m))
left = mid;
else
right = mid - 1;
}
return left;
}
bool check(vector<int>& position, int f, int m) {
int prev = position[0];
int cnt = 1;
for (int i = 1; i < position.size(); ++i) {
int curr = position[i];
if (curr - prev >= f) {
prev = curr;
++cnt;
}
}
return cnt >= m;
}
};
func maxDistance(position []int, m int) int {
sort.Ints(position)
left, right := 1, position[len(position)-1]
check := func(f int) bool {
prev, cnt := position[0], 1
for _, curr := range position[1:] {
if curr-prev >= f {
prev = curr
cnt++
}
}
return cnt >= m
}
for left < right {
mid := (left + right + 1) >> 1
if check(mid) {
left = mid
} else {
right = mid - 1
}
}
return left
}
/**
* @param {number[]} position
* @param {number} m
* @return {number}
*/
var maxDistance = function (position, m) {
position.sort((a, b) => {
return a - b;
});
let left = 1,
right = position[position.length - 1];
const check = function (f) {
let prev = position[0];
let cnt = 1;
for (let i = 1; i < position.length; ++i) {
const curr = position[i];
if (curr - prev >= f) {
prev = curr;
++cnt;
}
}
return cnt >= m;
};
while (left < right) {
const mid = (left + right + 1) >> 1;
if (check(mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
};