Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 803 Bytes

1608.特殊数组的特征值.md

File metadata and controls

42 lines (36 loc) · 803 Bytes

1608.特殊数组的特征值

/*
 * @lc app=leetcode.cn id=1608 lang=typescript
 *
 * [1608] 特殊数组的特征值
 */

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

解法 1: 排序 + 枚举

function specialArray(nums: number[]): number {
  const n = nums.length
  nums.sort((a, b) => a - b)
  for (let i = 0; i < n; i++) {
    const x = n - i
    if (x <= nums[i]) {
      if (!i || nums[i - 1] < x) return x
      else break
    }
  }
  return -1
}

Case

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