Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 770 Bytes

2418.按身高排序.md

File metadata and controls

32 lines (25 loc) · 770 Bytes

2418.按身高排序

/*
 * @lc app=leetcode.cn id=2418 lang=typescript
 *
 * [2418] 按身高排序
 */

// @lc code=start

// @lc code=end

解法 1: 排序

function sortPeople(names: string[], heights: number[]): string[] {
  return [...new Array(names.length).keys()].sort((a, b) => heights[b] - heights[a]).map(i => names[i])
}

Case

test.each([
  { input: { names: ['Mary', 'John', 'Emma'], heights: [180, 165, 170] }, output: ['Mary', 'Emma', 'John'] },
  { input: { names: ['Alice', 'Bob', 'Bob'], heights: [155, 185, 150] }, output: ['Bob', 'Alice', 'Bob'] },
])('input: names = $input.names, heights = $input.heights', ({ input: { names, heights }, output }) => {
  expect(sortPeople(names, heights)).toEqual(output)
})