[LeetCode] 2418. Sort the People

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 2418. Sort the People相关的知识,希望对你有一定的参考价值。

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实现

 class Solution 
     public String[] sortPeople(String[] names, int[] heights) 
         HashMap<Integer, String> map = new HashMap<>();
         int n = heights.length;
         for (int i = 0; i < n; i++) 
             map.put(heights[i], names[i]);
         
         
         Arrays.sort(heights);
         String[] res = new String[n];
         int index = 0;
         for (int i = heights.length - 1; i >= 0; i--) 
             res[index++] = map.get(heights[i]);
         
         return res;
     
 

 

LeetCode 题目总结

LeetCode --- 1356. Sort Integers by The Number of 1 Bits 解题报告

You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1\'s in their binary representation and in case of two or more integers have the same number of 1\'s you have to sort them in ascending order.

Return the array after sorting it.

Example 1:

Input: arr = [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
Explantion: [0] is the only integer with 0 bits.
[1,2,4,8] all have 1 bit.
[3,5,6] have 2 bits.
[7] has 3 bits.
The sorted array by bits is [0,1,2,4,8,3,5,6,7]

Example 2:

Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
Output: [1,2,4,8,16,32,64,128,256,512,1024]
Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.

以上是关于[LeetCode] 2418. Sort the People的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode | 1387. Sort Integers by The Power Value将整数按权重排序Python

LeetCode --- 1356. Sort Integers by The Number of 1 Bits 解题报告

Leetcode 1356. Sort Integers by The Number of 1 Bits

1356. Sort Integers by The Number of 1 Bits

Leetcode 451. Sort Characters By Frequency JAVA语言

find-the-difference