{"id":9363,"date":"2021-12-31T15:59:40","date_gmt":"2021-12-31T23:59:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9363"},"modified":"2021-12-31T16:04:08","modified_gmt":"2022-01-01T00:04:08","slug":"leetcode-1984-minimum-difference-between-highest-and-lowest-of-k-scores","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1984-minimum-difference-between-highest-and-lowest-of-k-scores\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1984. Minimum Difference Between Highest and Lowest of K Scores"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, where&nbsp;<code>nums[i]<\/code>&nbsp;represents the score of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;student. You are also given an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Pick the scores of any&nbsp;<code>k<\/code>&nbsp;students from the array so that the&nbsp;<strong>difference<\/strong>&nbsp;between the&nbsp;<strong>highest<\/strong>&nbsp;and the&nbsp;<strong>lowest<\/strong>&nbsp;of the&nbsp;<code>k<\/code>&nbsp;scores is&nbsp;<strong>minimized<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;possible difference<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [90], k = 1\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There is one way to pick score(s) of one student:\n- [<strong><u>90<\/u><\/strong>]. The difference between the highest and lowest score is 90 - 90 = 0.\nThe minimum possible difference is 0.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [9,4,1,7], k = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are six ways to pick score(s) of two students:\n- [<strong><u>9<\/u><\/strong>,<strong><u>4<\/u><\/strong>,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.\n- [<strong><u>9<\/u><\/strong>,4,<strong><u>1<\/u><\/strong>,7]. The difference between the highest and lowest score is 9 - 1 = 8.\n- [<strong><u>9<\/u><\/strong>,4,1,<strong><u>7<\/u><\/strong>]. The difference between the highest and lowest score is 9 - 7 = 2.\n- [9,<strong><u>4<\/u><\/strong>,<strong><u>1<\/u><\/strong>,7]. The difference between the highest and lowest score is 4 - 1 = 3.\n- [9,<strong><u>4<\/u><\/strong>,1,<strong><u>7<\/u><\/strong>]. The difference between the highest and lowest score is 7 - 4 = 3.\n- [9,4,<strong><u>1<\/u><\/strong>,<strong><u>7<\/u><\/strong>]. The difference between the highest and lowest score is 7 - 1 = 6.\nThe minimum possible difference is 2.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Sort the array, to minimize the difference, k numbers must be consecutive (i.e, from a subarray). We use a sliding window size of k and try all possible subarrays. <br>Ans = min{(nums[k &#8211; 1] &#8211; nums[0]), (nums[k] &#8211; nums[1]), &#8230; (nums[n &#8211; 1] &#8211; nums[n &#8211; k])}<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumDifference(vector<int>& nums, int k) {\n    int ans = INT_MAX;\n    sort(begin(nums), end(nums));\n    for (int i = k - 1; i < nums.size(); ++i)\n      ans = min(ans, nums[i] - nums[i - k + 1]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, where&nbsp;nums[i]&nbsp;represents the score of the&nbsp;ith&nbsp;student. You are also given an integer&nbsp;k. Pick the scores of any&nbsp;k&nbsp;students from the array so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[222,215,23],"class_list":["post-9363","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-easy","tag-sliding-window","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9363"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9363\/revisions"}],"predecessor-version":[{"id":9366,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9363\/revisions\/9366"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}