#leetcode# 1984. Minimum Difference Between Highest and Lowest of K Scores

1984. Minimum Difference Between Highest and Lowest of K Scores1984. Minimum Difference Between Highest and Lowest of K Scores1984. Minimum Difference Between Highest and Lowest of K Scores

Easy

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

06年看看之前写的解题文章,居然一共有10w+浏览量了, 2026年了,跳槽还得刷题,不只是该作何感想,多想无益,这次用Python,AI时代,换主力语言


这个题,暴力解法就是遍历所有的组合可能,然后分别求最小的difference,太久不做算法提了,其他什么都想不出来。。。big O 也想不出来,直接看答案学习了


官方的hint已经很精辟了

先排序,然后维护一个长度为k的sliding window,遍历一遍即可找到答案

python 复制代码
class Solution:
    def minimumDifference(self, nums: List[int], k: int) -> int:
        sortedNums = sorted(nums)
        start = 0
        l = len(sortedNums)
        res = sortedNums[l - 1] - sortedNums[0]
        for start in range(0, l - k + 1):
            res = min(res, sortedNums[start + k - 1] - sortedNums[start])
        return res

我的代码如上,对Python还不是特别熟悉,不够简练,另外range,以及start end index的设置也费了点脑子,用一个例子比用脑子想容易很多

比如

python 复制代码
0, 1, 2, 3 # index

1, 2, 3, 4 # size

k = 2

start from 0 to 2, range (0, 3)
2 = 4 - 2 
when start = 2, end  = 3, end  = start + k - 1

让GPT评价了一下,优化如下

Your code is correct and idiomatic for the standard solution (sort + fixed-size window).

What's good

  • Correct logic: after sorting, the minimum difference among any size-k subset is the minimum of a[i+k-1] - a[i].

  • Complexity: O(n log n) time for sorting + O(n) scan; O(n) extra space because sorted() returns a new list.

  • Clean window indexing; no off-by-one issues.

Small improvements

  1. start = 0 before the loop is unused (can remove).

  2. Use clearer names + handle edge cases explicitly (optional).

A slightly cleaner version:

python 复制代码
class Solution:
    def minimumDifference(self, nums: List[int], k: int) -> int:
        if k <= 1:
            return 0

        nums.sort()  # in-place, saves memory vs sorted(nums)
        res = float("inf")

        for i in range(len(nums) - k + 1):
            res = min(res, nums[i + k - 1] - nums[i])

        return res

Opinion: Prefer nums.sort() over sorted(nums) in LeetCode unless you need to preserve the original list.

确实更简洁

相关推荐
仰泳的熊猫2 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
无极低码6 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发6 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
罗超驿6 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
superior tigre7 小时前
22 括号生成
算法·深度优先
努力也学不会java8 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
旖-旎8 小时前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针
ECT-OS-JiuHuaShan8 小时前
朱梁万有递归元定理,重构《易经》
算法·重构
智者知已应修善业9 小时前
【51单片机独立按键控制数码管移动反向,2片74CH573/74CH273段和位,按键按下保持原状态】2023-3-25
经验分享·笔记·单片机·嵌入式硬件·算法·51单片机