1984. Minimum Difference Between Highest and Lowest of K Scores Easy TopicsCompanies…
- User
- Minimum Difference Between Highest and Lowest of K Scores Easy TopicsCompanies Hint 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
Q- what is can you explain the problem using your own words ? 7
So we are given a list of integers: List[int], k: int we need to find the best sub_sequence where the difference between the max_value and min_value is “minimized”
Q- what is the input and output format?
Input : List[int] , int Output: int
Q- can you go through an example?
Input: nums = [9,4,1,7], k = 2
Q- before I continue? I have one principle in my mind.
If you do not know the optimized solution directly you need first to solve it with naive solution , compute the time complexity & space complexity And optimize it
Q- let’s return to the example.
First we need to compute all the sub_sequence possible oky of length k
For example [9,4,1,7] The first one [9,4] , [9,1] , [9,7] “Please complete all the other one for me “
Okay, completing the subsequences of length 2 (k=2) from nums = [9,4,1,7]: [9, 4] Difference: 9 - 4 = 5 [9, 1] Difference: 9 - 1 = 8 [9, 7] Difference: 9 - 7 = 2 [4, 1] Difference: 4 - 1 = 3 [4, 7] Difference: 7 - 4 = 3 [1, 7] Difference: 7 - 1 = 6
Next for each possible subSequences: compute the silence between the max , min value in it
Then update our min_value based on it .. and return back oky
Q- how algorithmically can implement this naive solution ? I think we need to use two iterator i and j Where i from 0 to nums.length() And j from j to nums.length() Compute max_value in this subSequence Compute min_value in this subSequence Compute diff Next we need now to compare it with the min_value with it Update based on this value
- Assistant