返回介绍

solution / 1900-1999 / 1984.Minimum Difference Between Highest and Lowest of K Scores / README_EN

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

1984. Minimum Difference Between Highest and Lowest of K Scores

中文文档

Description

You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k.

Pick the scores of any k students from the array so that the difference between the highest and the lowest of the k scores is minimized.

Return _the minimum possible difference_.

 

Example 1:

Input: nums = [90], k = 1
Output: 0
Explanation: There is one way to pick score(s) of one student:
- [90]. The difference between the highest and lowest score is 90 - 90 = 0.
The minimum possible difference is 0.

Example 2:

Input: nums = [9,4,1,7], k = 2
Output: 2
Explanation: There are six ways to pick score(s) of two students:
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2.
- [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6.
The minimum possible difference is 2.

 

Constraints:

  • 1 <= k <= nums.length <= 1000
  • 0 <= nums[i] <= 105

Solutions

Solution 1

class Solution:
  def minimumDifference(self, nums: List[int], k: int) -> int:
    nums.sort()
    return min(nums[i + k - 1] - nums[i] for i in range(len(nums) - k + 1))
class Solution {
  public int minimumDifference(int[] nums, int k) {
    Arrays.sort(nums);
    int ans = 100000;
    for (int i = 0; i < nums.length - k + 1; ++i) {
      ans = Math.min(ans, nums[i + k - 1] - nums[i]);
    }
    return ans;
  }
}
class Solution {
public:
  int minimumDifference(vector<int>& nums, int k) {
    sort(nums.begin(), nums.end());
    int ans = 1e5;
    for (int i = 0; i < nums.size() - k + 1; ++i)
      ans = min(ans, nums[i + k - 1] - nums[i]);
    return ans;
  }
};
func minimumDifference(nums []int, k int) int {
  sort.Ints(nums)
  ans := 100000
  for i := 0; i < len(nums)-k+1; i++ {
    ans = min(ans, nums[i+k-1]-nums[i])
  }
  return ans
}
function minimumDifference(nums: number[], k: number): number {
  nums.sort((a, b) => a - b);
  const n = nums.length;
  let ans = nums[n - 1] - nums[0];
  for (let i = 0; i + k - 1 < n; i++) {
    ans = Math.min(nums[i + k - 1] - nums[i], ans);
  }
  return ans;
}
impl Solution {
  pub fn minimum_difference(mut nums: Vec<i32>, k: i32) -> i32 {
    nums.sort();
    let k = k as usize;
    let mut res = i32::MAX;
    for i in 0..=nums.len() - k {
      res = res.min(nums[i + k - 1] - nums[i]);
    }
    res
  }
}
class Solution {
  /**
   * @param Integer[] $nums
   * @param Integer $k
   * @return Integer
   */
  function minimumDifference($nums, $k) {
    sort($nums);
    $rs = 10 ** 5;
    for ($i = 0; $i < count($nums) - $k + 1; $i++) {
      $rs = min($rs, $nums[$i + $k - 1] - $nums[$i]);
    }
    return $rs;
  }
}

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

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

发布评论

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