首页 > 其他分享 >[LeetCode] 2418. Sort the People

[LeetCode] 2418. Sort the People

时间:2023-04-25 23:44:38浏览次数:43  
标签:Sort String int heights length names 2418 Bob 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] <= 105
  • names[i] consists of lower and upper case English letters.
  • All the values of heights are distinct.

按身高排序。

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sort-the-people
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是排序。这里我们可以用一个 hashmap 存每个人的<身高,名字>。然后我们对 heights 数组进行排序并从大到小遍历。遍历的时候将每个身高在 hashmap 中对应的名字放入结果集。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public String[] sortPeople(String[] names, int[] heights) {
 3         HashMap<Integer, String> map = new HashMap<>();
 4         int n = heights.length;
 5         for (int i = 0; i < n; i++) {
 6             map.put(heights[i], names[i]);
 7         }
 8         
 9         Arrays.sort(heights);
10         String[] res = new String[n];
11         int index = 0;
12         for (int i = heights.length - 1; i >= 0; i--) {
13             res[index++] = map.get(heights[i]);
14         }
15         return res;
16     }
17 }

 

LeetCode 题目总结

标签:Sort,String,int,heights,length,names,2418,Bob,LeetCode
From: https://www.cnblogs.com/cnoodle/p/17354377.html

相关文章

  • 【LeetCode动态规划#13】买卖股票含冷冻期(状态众多,比较繁琐)、含手续费
    最佳买卖股票时机含冷冻期力扣题目链接(opensnewwindow)给定一个整数数组,其中第i个元素代表了第i天的股票价格。设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):你不能同时参与多笔交易(你必须在再次购买前出售掉之前......
  • LeetCode 1643. 第 K 条最小指令
    康托展开一开始无脑枚举全排列,果断超时,还是得看看如果降低计算量。题目destination=[2,3],相当于2个V,3个H,输出全排列去重后的对应位置字典序列内容。忽略去重则问题为全排列,所有可能为:\[(\sumdestination)!=(2+3)!=5!\]k恰好为康托展开结果+1,直接逆向......
  • leetcode调研version0
     这是我第一次发博客,所以许多功能还不太会使用。前几次的随笔既当作记录,也当作自己的练习。 最近想要刷leetcode,纠结用哪种语言(我自己学过c/c++,python,fortran,Java),所以前期做了一些调研,在此记录一下。 c语言: 网址:https://github.com/begeekmyfriend/leetcode ......
  • leetcode 607 銷售員
    銷售員 selects.`name`fromsalespersonsleftjoinordersoons.sales_id=o.sales_idleftjoincompanycono.com_id=c.com_idandc.name='RED'groupbys.sales_idhavingcount(c.`name`)=0 ===selects.`name`fromsalespersonswheres.sa......
  • 【DP】LeetCode 213. 打家劫舍 II
    题目链接213.打家劫舍II思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums以前i个元素组成(即nums[i-1])的状态;dp[i][j]分别表示以nums1前i个元素(即n......
  • leetcode 570 至少有5名直接下屬的經理
    至少有5名直接下屬的經理 子查詢select`name`fromEmployeewhereidin(selectmanagerIdfromEmployeegroupbymanagerIdhavingcount(managerId)>=5) 自連接selecte2.namefromEmployeee1,Employeee2wheree1.managerId=e2.idgr......
  • [LeetCode] 1342. Number of Steps to Reduce a Number to Zero 将数字变成 0 的操作
    Givenaninteger num,return thenumberofstepstoreduceittozero.Inonestep,ifthecurrentnumberiseven,youhavetodivideitby 2,otherwise,youhavetosubtract 1 fromit.Example1:Input:num=14Output:6Explanation: Step1)14ise......
  • LeetCode 131.分割回文串
    1.题目:给你一个字符串s,请你将s分割成一些子串,使每个子串都是回文串。返回s所有可能的分割方案。回文串是正着读和反着读都一样的字符串。示例1:输入:s="aab"输出:[["a","a","b"],["aa","b"]]示例2:输入:s="a"输出:[["a"]]来源:力扣(LeetCode)链接:https......
  • 【DP】LeetCode 198. 打家劫舍
    题目链接198.打家劫舍思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums以前i个元素组成(即nums[i-1])的状态;dp[i][j]分别表示以nums1前i个元素(即nums......
  • leetcode(2)
    Given n pointsona2Dplane,findthemaximumnumberofpointsthatlieonthesamestraightline.在一条直线上最多的点。理解错了:理解成一条直线上距离最长的点(自己见过类似题,务必审题要细!!!!)4.1hash_map和map的区别在哪里?构造函数。hash_map需要hash函数,......