• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

LeetCode 2418. Sort the People排序,哈希表

武飞扬头像
memcpy0
帮助1

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

For each index inames[i] and heights[i] denote the name and height of the ith person.

Return names sorted in descending order by the people’s heights.

Example 1:

Input: names = ["Mary","John","Emma"], heights = [180,165,170]
Output: ["Mary","Emma","John"]
Explanation: Mary is the tallest, followed by Emma and John.

Example 2:

Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
Output: ["Bob","Alice","Bob"]
Explanation: The first Bob is the tallest, followed by Alice and the second Bob.

Constraints:

  • n == names.length == heights.length
  • 1 <= n <= 103
  • 1 <= names[i].length <= 20
  • 1 <= heights[i] <= 10^5
  • names[i] consists of lower and upper case English letters.
  • All the values of heights are distinct.

题意:对于每个下标 inames[i] 和 heights[i] 表示第 i 个人的名字和身高。请按身高 降序 顺序返回对应的名字数组 names 。


解法1 对下标数组排序

通用做法是创建一个下标数组,对下标数组排序,这样既不会打乱输入的数组,又保证了 n a m e s [ i ] names[i] names[i] h e i g h t s [ i ] heights[i] heights[i] 的对应关系。

class Solution {
public:
    vector<string> sortPeople(vector<string> &names, vector<int> &heights) {
        int n = names.size(), id[n];
        iota(id, id   n, 0);
        sort(id, id   n, [&](const auto &i, const auto &j) {
            return heights[i] > heights[j];
        });
        vector<string> ans(n);
        for (int i = 0; i < n;   i)
            ans[i] = names[id[i]];
        return ans;
    }
};

复杂度分析:

  • 时间复杂度: O ( n log ⁡ n ) O(n\log n) O(nlogn)
  • 空间复杂度: O ( n ) O(n) O(n)

解法2 哈希表

由于身高各不相同,所以可以使用哈希表记录每个身高所对应的下标,最后从高到低收集对应名字即可:

class Solution {
public:
    vector<string> sortPeople(vector<string> &names, vector<int> &heights) {
        int n = names.size(), maxn = *max_element(heights.begin(), heights.end());
        int rec[maxn   1]; memset(rec, -1, sizeof(rec));
        for (int i = 0; i < n;   i) rec[heights[i]] = i;
        vector<string> ans;
        for (int i = maxn; i >= 1; --i)
            if (rec[i] != -1) ans.push_back(names[rec[i]]);
        return ans;
    }
};

复杂度分析:

  • 时间复杂度: O ( m a x n ) O(maxn) O(maxn)
  • 空间复杂度: O ( m a x n ) O(maxn) O(maxn)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhbag
系列文章
更多 icon
同类精品
更多 icon
继续加载