Skip to content

Latest commit

 

History

History
41 lines (35 loc) · 824 Bytes

1403.非递增顺序的最小子序列.md

File metadata and controls

41 lines (35 loc) · 824 Bytes

1403.非递增顺序的最小子序列

/*
 * @lc app=leetcode.cn id=1403 lang=typescript
 *
 * [1403] 非递增顺序的最小子序列
 */

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

解法 1: 排序

function minSubsequence(nums: number[]): number[] {
  nums.sort((a, b) => b - a)
  const sum = nums.reduce((a, b) => a + b, 0)
  let res = 0
  for (let i = 0; i < nums.length; i++) {
    res += nums[i]
    if (res > sum / 2) {
      return nums.slice(0, i + 1)
    }
  }
  return []
}

Case

test.each([
  { input: { nums: [4, 3, 10, 9, 8] }, output: [10, 9] },
  { input: { nums: [4, 4, 7, 6, 7] }, output: [7, 7, 6] },
])('input: nums = $input.nums', ({ input: { nums }, output }) => {
  expect(minSubsequence(nums)).toEqual(output)
})