diff --git a/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README.md b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README.md index 065724c595368..870e79920b985 100644 --- a/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README.md +++ b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README.md @@ -57,7 +57,15 @@ ```python - +class Solution: + def minimumDifference(self, nums: List[int], k: int) -> int: + if k == 1: + return 0 + nums.sort() + ans = maxsize + for i in range(len(nums) - k + 1): + ans = min(ans, nums[i + k - 1] - nums[i]) + return ans ``` ### **Java** @@ -65,7 +73,19 @@ ```java - +class Solution { + public int minimumDifference(int[] nums, int k) { + if (k == 1) { + return 0; + } + Arrays.sort(nums); + int min = Integer.MAX_VALUE; + for (int i = 0; i < nums.length - k + 1; i++) { + min = Math.min((nums[i + k - 1] - nums[i]), min); + } + return min; + } +} ``` ### **...** diff --git a/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README_EN.md b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README_EN.md index 09ee01c91f2ff..4c69495b55332 100644 --- a/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README_EN.md +++ b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README_EN.md @@ -51,13 +51,33 @@ The minimum possible difference is 2. ### **Python3** ```python - +class Solution: + def minimumDifference(self, nums: List[int], k: int) -> int: + if k == 1: + return 0 + nums.sort() + ans = maxsize + for i in range(len(nums) - k + 1): + ans = min(ans, nums[i + k - 1] - nums[i]) + return ans ``` ### **Java** ```java - +class Solution { + public int minimumDifference(int[] nums, int k) { + if (k == 1) { + return 0; + } + Arrays.sort(nums); + int min = Integer.MAX_VALUE; + for (int i = 0; i < nums.length - k + 1; i++) { + min = Math.min((nums[i + k - 1] - nums[i]), min); + } + return min; + } +} ``` ### **...** diff --git a/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/Solution.java b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/Solution.java new file mode 100644 index 0000000000000..3d69f0b26548f --- /dev/null +++ b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int minimumDifference(int[] nums, int k) { + if (k == 1) { + return 0; + } + Arrays.sort(nums); + int min = Integer.MAX_VALUE; + for (int i = 0; i < nums.length - k + 1; i++) { + min = Math.min((nums[i + k - 1] - nums[i]), min); + } + return min; + } +} \ No newline at end of file diff --git a/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/Solution.py b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/Solution.py new file mode 100644 index 0000000000000..0d2adfb08e72d --- /dev/null +++ b/solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minimumDifference(self, nums: List[int], k: int) -> int: + if k == 1: + return 0 + nums.sort() + ans = maxsize + for i in range(len(nums) - k + 1): + ans = min(ans, nums[i + k - 1] - nums[i]) + return ans \ No newline at end of file