Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1582 from aadil42/patch-34
Browse files Browse the repository at this point in the history
  • Loading branch information
aakhtar3 authored Dec 26, 2022
2 parents c3a07f5 + cd12ccc commit e1c63df
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Loglinear/N*log(N)
* Time O(N*log(N)) | Space O(1)
* https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores
*
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minimumDifference = function(nums, k) {

const isEdgeCase = (k === 1);
if (isEdgeCase) return 0;

nums = nums.sort((a, b) => {
return a - b;
});

let i = 0;
let j = k - 1;
let minDiffrence = Infinity;

while (j < nums.length) {
minDiffrence = Math.min(Math.abs(nums[i] - nums[j]), minDiffrence);
j++;
i++;
}

return minDiffrence;
};

0 comments on commit e1c63df

Please sign in to comment.