Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 1984-Minimum-Difference-Between-Highest-and-Lowest-of-K-Scores.js #1582

Merged
merged 4 commits into from
Dec 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create 1984-Minimum-Difference-Between-Highest-and-Lowest-of-K-Scores.js
Solved Minimum Difference Between Highest and Lowest of K Scores in JS.
  • Loading branch information
aadil42 committed Dec 23, 2022
commit 4ac2297c8c7413496ab73ea8aba67e516a98d5a6
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Logarithmic time.
aadil42 marked this conversation as resolved.
Show resolved Hide resolved
* Time O(N + N*long(N)) | Space O(1)
aadil42 marked this conversation as resolved.
Show resolved Hide resolved
* 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) {

if (k === 1) return 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be descriptive with your conditions

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;
};