返回介绍

solution / 1500-1599 / 1509.Minimum Difference Between Largest and Smallest Value in Three Moves / README_EN

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

1509. Minimum Difference Between Largest and Smallest Value in Three Moves

中文文档

Description

You are given an integer array nums.

In one move, you can choose one element of nums and change it to any value.

Return _the minimum difference between the largest and smallest value of nums after performing at most three moves_.

 

Example 1:

Input: nums = [5,3,2,4]
Output: 0
Explanation: We can make at most 3 moves.
In the first move, change 2 to 3. nums becomes [5,3,3,4].
In the second move, change 4 to 3. nums becomes [5,3,3,3].
In the third move, change 5 to 3. nums becomes [3,3,3,3].
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.

Example 2:

Input: nums = [1,5,0,10,14]
Output: 1
Explanation: We can make at most 3 moves.
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
It can be shown that there is no way to make the difference 0 in 3 moves.

Example 3:

Input: nums = [3,100,20]
Output: 0
Explanation: We can make at most 3 moves.
In the first move, change 100 to 7. nums becomes [3,7,20].
In the second move, change 20 to 7. nums becomes [3,7,7].
In the third move, change 3 to 7. nums becomes [7,7,7].
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.

 

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def minDifference(self, nums: List[int]) -> int:
    n = len(nums)
    if n < 5:
      return 0
    nums.sort()
    ans = inf
    for l in range(4):
      r = 3 - l
      ans = min(ans, nums[n - 1 - r] - nums[l])
    return ans
class Solution {
  public int minDifference(int[] nums) {
    int n = nums.length;
    if (n < 5) {
      return 0;
    }
    Arrays.sort(nums);
    long ans = 1L << 60;
    for (int l = 0; l <= 3; ++l) {
      int r = 3 - l;
      ans = Math.min(ans, (long) nums[n - 1 - r] - nums[l]);
    }
    return (int) ans;
  }
}
class Solution {
public:
  int minDifference(vector<int>& nums) {
    int n = nums.size();
    if (n < 5) {
      return 0;
    }
    sort(nums.begin(), nums.end());
    long long ans = 1L << 60;
    for (int l = 0; l <= 3; ++l) {
      int r = 3 - l;
      ans = min(ans, 1LL * nums[n - 1 - r] - nums[l]);
    }
    return ans;
  }
};
func minDifference(nums []int) int {
  n := len(nums)
  if n < 5 {
    return 0
  }
  sort.Ints(nums)
  ans := 1 << 60
  for l := 0; l <= 3; l++ {
    r := 3 - l
    ans = min(ans, nums[n-1-r]-nums[l])
  }
  return ans
}

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

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

发布评论

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