LeetCode 题解工作台

按身高排序

给你一个字符串数组 names ,和一个由 互不相同 的正整数组成的数组 heights 。两个数组的长度均为 n 。 对于每个下标 i , names[i] 和 heights[i] 表示第 i 个人的名字和身高。 请按身高 降序 顺序返回对应的名字数组 names 。 示例 1: 输入: nam…

category

4

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·哈希·扫描

bolt

答案摘要

根据题目描述,我们可以创建一个长度为 的下标数组 ,其中 。然后我们对 中的每个下标按照 中对应的身高降序排序,最后遍历排序后的 中的每个下标 ,将 加入答案数组即可。 我们也可以创建一个长度为 的数组 ,数组中每个元素是一个二元组 $(heights[i], i)$,然后我们对 按照身高降序排序。最后遍历排序后的 中的每个元素 $(heights[i], i)$,将 加入答案数…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串数组 names ,和一个由 互不相同 的正整数组成的数组 heights 。两个数组的长度均为 n

对于每个下标 inames[i]heights[i] 表示第 i 个人的名字和身高。

请按身高 降序 顺序返回对应的名字数组 names

 

示例 1:

输入:names = ["Mary","John","Emma"], heights = [180,165,170]
输出:["Mary","Emma","John"]
解释:Mary 最高,接着是 Emma 和 John 。

示例 2:

输入:names = ["Alice","Bob","Bob"], heights = [155,185,150]
输出:["Bob","Alice","Bob"]
解释:第一个 Bob 最高,然后是 Alice 和第二个 Bob 。

 

提示:

  • n == names.length == heights.length
  • 1 <= n <= 103
  • 1 <= names[i].length <= 20
  • 1 <= heights[i] <= 105
  • names[i] 由大小写英文字母组成
  • heights 中的所有值互不相同
lightbulb

解题思路

方法一:排序

根据题目描述,我们可以创建一个长度为 nn 的下标数组 idxidx,其中 idx[i]=iidx[i]=i。然后我们对 idxidx 中的每个下标按照 heightsheights 中对应的身高降序排序,最后遍历排序后的 idxidx 中的每个下标 ii,将 names[i]names[i] 加入答案数组即可。

我们也可以创建一个长度为 nn 的数组 arrarr,数组中每个元素是一个二元组 (heights[i],i)(heights[i], i),然后我们对 arrarr 按照身高降序排序。最后遍历排序后的 arrarr 中的每个元素 (heights[i],i)(heights[i], i),将 names[i]names[i] 加入答案数组即可。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 是数组 namesnamesheightsheights 的长度。

1
2
3
4
5
6
class Solution:
    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
        idx = list(range(len(heights)))
        idx.sort(key=lambda i: -heights[i])
        return [names[i] for i in idx]
speed

复杂度分析

指标
时间complexity is O(n \cdot \log n) due to the sorting of n elements. Space complexity is O(n) because we create an auxiliary array of tuples pairing names and heights.
空间O(n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Expect a direct mapping of names to heights and correct descending sort order.

  • question_mark

    Be prepared to explain why a hash or tuple-based approach avoids mixing names and heights.

  • question_mark

    Highlight the pattern of scanning arrays and using auxiliary structures for sorting without losing associations.

warning

常见陷阱

外企场景
  • error

    Sorting names and heights separately without pairing can misalign names with heights.

  • error

    Assuming heights are not distinct and trying to handle duplicates unnecessarily.

  • error

    Forgetting to return only names in the final output instead of height-name pairs.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Sort by other attributes such as weight or age while keeping name mapping intact.

  • arrow_right_alt

    Handle duplicate heights by breaking ties alphabetically or by original index.

  • arrow_right_alt

    Sort names in ascending height order instead of descending.

help

常见问题

外企场景

按身高排序题解:数组·哈希·扫描 | LeetCode #2418 简单