Skip to content

Latest commit

 

History

History
37 lines (31 loc) · 791 Bytes

1984.学生分数的最小差值.md

File metadata and controls

37 lines (31 loc) · 791 Bytes

1984.学生分数的最小差值

/*
 * @lc app=leetcode.cn id=1984 lang=typescript
 *
 * [1984] 学生分数的最小差值
 */

// @lc code=start
function minimumDifference(nums: number[], k: number): number {}
// @lc code=end

解法 1: 排序 + 双指针

function minimumDifference(nums: number[], k: number): number {
  nums.sort((a, b) => a - b)
  let res = Infinity
  for (let i = 0, j = k - 1; j < nums.length; i++, j++) {
    res = Math.min(res, nums[j] - nums[i])
  }
  return res
}

Case

test.each([
  { input: { nums: [90], k: 1 }, output: 0 },
  { input: { nums: [9, 4, 1, 7], k: 2 }, output: 2 },
])('input: nums = $input.nums, k = $input.k', ({ input: { nums, k }, output }) => {
  expect(minimumDifference(nums, k)).toEqual(output)
})