LOADING

加载过慢请开启缓存 浏览器默认开启

leetcode 2418

题目:

给你一个字符串数组 names ,和一个由 互不相同 的正整数组成的数组 heights 。两个数组的长度均为 n 。对于每个下标 i,names[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 中的所有值互不相同

解决方法

#include <bits/stdc++.h>
using namespace std;
class Solution
{
    struct compare
    {
        bool operator()(const int &a, const int &b) const
        {
            return a > b;
        }
    };

public:
    vector<string> sortPeople(vector<string> &names, vector<int> &heights)
    {
        map<int, string, compare> mp;
        for (int i = 0; i < names.size(); i++)
        {
            mp.insert(make_pair(heights[i], names[i]));
        }
        vector<string> ans;
        for (auto &num : mp)
        {
            ans.push_back(num.second);
        }
        return ans;
    }
};
本文作者:GWB
当前时间:2023-11-09 11:11:11
版权声明:本文由gwb原创,本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 国际许可协议。
转载请注明出处!