1552. Magnetic Force Between Two Balls Medium
1/**
2 * [1552] Magnetic Force Between Two Balls
3 *
4 * 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 i^th 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.
5 * Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
6 * Given the integer array position and the integer m. Return the required force.
7 *
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg" style="width: 562px; height: 195px;" />
10 * Input: position = [1,2,3,4,7], m = 3
11 * Output: 3
12 * 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.
13 *
14 * Example 2:
15 *
16 * Input: position = [5,4,3,2,1,1000000000], m = 2
17 * Output: 999999999
18 * Explanation: We can use baskets 1 and 1000000000.
19 *
20 *
21 * Constraints:
22 *
23 * n == position.length
24 * 2 <= n <= 10^5
25 * 1 <= position[i] <= 10^9
26 * All integers in position are distinct.
27 * 2 <= m <= position.length
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/magnetic-force-between-two-balls/
33// discuss: https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn max_distance(position: Vec<i32>, m: i32) -> i32 {
39 0
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_1552() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.