Skip to content

Latest commit

 

History

History
38 lines (32 loc) · 761 Bytes

2465.不同的平均值数目.md

File metadata and controls

38 lines (32 loc) · 761 Bytes

2465.不同的平均值数目

/*
 * @lc app=leetcode.cn id=2465 lang=typescript
 *
 * [2465] 不同的平均值数目
 */

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

解法 1: 排序+双指针

function distinctAverages(nums: number[]): number {
  const set = new Set<number>()
  nums.sort((a, b) => a - b)
  const n = nums.length
  for (let i = 0, j = n - 1; i <= j; i++, j--) {
    set.add((nums[i] + nums[j]) / 2)
  }
  return set.size
}

Case

test.each([
  { input: { nums: [4, 1, 4, 0, 3, 5] }, output: 2 },
  { input: { nums: [1, 100] }, output: 1 },
])('input: nums = $input.nums', ({ input: { nums }, output }) => {
  expect(distinctAverages(nums)).toEqual(output)
})