首页 > 其他分享 >leetcode-1984-easy

leetcode-1984-easy

时间:2022-11-04 19:11:26浏览次数:53  
标签:lowest 1984 highest int score easy between difference leetcode

Minimum Difference Between Highest and Lowest of K Scores

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

思路一:先排序,然后对比 k 间距的两数之间的值。刚开始想歪了,用 k 个不同的起点遍历,每次自增的跨度是 k,虽然通过了,但是用了双层循环,代码看起来比较丑。看了题解,发现一次遍历就行,把 k 的间距当成滑动窗口,在数组上遍历,最小值就是所求解。

    public int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums);

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < k; i++) {
            for (int j = i; j <= nums.length - k; j+=k) {
                min = Math.min(nums[j + k - 1] - nums[j], min);
            }
        }

        return min;
    }
public int minimumDifference(int[] nums, int k) {
    Arrays.sort(nums);

    int min = Integer.MAX_VALUE;
    for (int j = 0; j <= nums.length - k; j++) {
        min = Math.min(nums[j + k - 1] - nums[j], min);
    }

    return min;
}

标签:lowest,1984,highest,int,score,easy,between,difference,leetcode
From: https://www.cnblogs.com/iyiluo/p/16858849.html

相关文章

  • EasyCVR国标GB28181协议接入下的TCP和UDP模式说明及差异
    有用户在使用我们的平台时,经常会出现对于端口的疑问,同时也不了解端口的差别。今天我们来解释说明下EasyCVR平台关于国标GB28181协议接入下的TCP和UDP模式的说明及差异。......
  • 视频融合平台EasyCVR各项数据正常,却无法用海康NVR接入是什么原因?
    EasyCVR视频融合云平台开放度高、兼容性强、可支持灵活拓展与第三方集成,目前已经成为安防市场主流的视频能力层服务平台。平台可支持多协议、多类型的设备接入,包括国标GB28......
  • 我用EasyExcel优化了公司的导出(附踩坑记录)
    背景介绍最近要改一个导出的功能,在原有的基础上,在导出一份明细数据,要求导出内容加在原有excel的第二个sheet上。考虑到数据量还比较大,干脆引入阿里的EasyExcel来做......
  • leetcode 160. 相交链表 js 实现
    给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。图示两个链表在节点 c1 开始相交: ......
  • leetcode-136. 只出现一次的数字
    题目描述给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明说明:你的算法应该具有线性时间复杂度。你可以不......
  • LeetCode刷题记录.Day5
    反转链表题目链接206.反转链表-力扣(LeetCode)classSolution{public:ListNode*reverseList(ListNode*head){ListNode*temp;ListNode*c......
  • leetcode java 杨辉三角
    简介杨辉三角是一道简单题,可以通过类似一层推下一层的方式进行计算,但是好像看过一个题解,采用的方式是组合数。本来想采用组合数,尝试了double溢出尝试了long溢出,尝试......
  • LeetCode_Stack_589. N-ary Tree Preorder Traversal N 叉树的前序遍历【栈,迭代】【简
    目录​​一,题目描述​​​​英文描述​​​​中文描述​​​​示例与说明​​​​二,解题思路​​​​三,AC代码​​​​C++​​​​Java​​​​四,解题过程​​​​第一博​......
  • jqueryeasyui-datagrid-扩展-支持单元格编辑
    jqueryeasyui-datagrid很强大,可是在包含三五十个列的时候,datagrid行编辑器加载就忍无可忍了(这里用的IE浏览器,其他的浏览器稍微好些)因为写了一个日期&时间的编辑器的扩......
  • [2021N1CTF国际赛]Easyphp-Wp
    文章目录​​写在前面​​​​Wp​​​​简单分析​​​​小实验验证猜想可效性​​​​构造tar​​​​参考文章​​写在前面在各种带飞的情况下,web被带ak了,这里比较想写一......