返回介绍

solution / 1500-1599 / 1552.Magnetic Force Between Two Balls / README_EN

发布于 2024-06-17 01:03:18 字数 5560 浏览 0 评论 0 收藏 0

1552. Magnetic Force Between Two Balls

中文文档

Description

In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

Given the integer array position and the integer m. Return _the required force_.

 

Example 1:

Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.

Example 2:

Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.

 

Constraints:

  • n == position.length
  • 2 <= n <= 105
  • 1 <= position[i] <= 109
  • All integers in position are distinct.
  • 2 <= m <= position.length

Solutions

Solution 1

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;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文