|
| 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 | + */ |
| 30 | +pub 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 | + |
| 37 | +impl Solution { |
| 38 | + pub fn max_distance(position: Vec<i32>, m: i32) -> i32 { |
| 39 | + let mut position = position; |
| 40 | + |
| 41 | + position.sort_unstable(); |
| 42 | + |
| 43 | + let mut left = 1; |
| 44 | + let mut right = position[position.len() - 1] - position[0]; |
| 45 | + |
| 46 | + while left < right { |
| 47 | + let mid = (left + right + 1) / 2; |
| 48 | + let mut count = 1; |
| 49 | + let mut last = position[0]; |
| 50 | + |
| 51 | + for &pos in position.iter().skip(1) { |
| 52 | + if pos - last >= mid { |
| 53 | + count += 1; |
| 54 | + last = pos; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if count >= m { |
| 59 | + left = mid; |
| 60 | + } else { |
| 61 | + right = mid - 1; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + left |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// submission codes end |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::*; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_1552_example_1() { |
| 77 | + let position = vec![1, 2, 3, 4, 7]; |
| 78 | + let m = 3; |
| 79 | + |
| 80 | + let result = 3; |
| 81 | + |
| 82 | + assert_eq!(Solution::max_distance(position, m), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_1552_example_2() { |
| 87 | + let position = vec![5, 4, 3, 2, 1, 1000000000]; |
| 88 | + let m = 2; |
| 89 | + |
| 90 | + let result = 999999999; |
| 91 | + |
| 92 | + assert_eq!(Solution::max_distance(position, m), result); |
| 93 | + } |
| 94 | +} |
0 commit comments