Algorithm/LeetCode

2418. Sort the People

 

https://leetcode.com/problems/sort-the-people/

 

Sort the People - LeetCode

Can you solve this real interview question? 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 i, names[i] and heights[i] denote the name

leetcode.com

 

문자열 이름의 배열과 고유한 양의 정수로 구성된 배열 높이가 제공됩니다.
 두 배열의 길이는 n입니다.
각 인덱스 i에 대해 names[i] 및 heights[i]는 i번째 사람의 이름과 키를 나타냅니다.
사람들의 키를 기준으로 내림차순으로 정렬된 이름을 반환합니다.

 

 

1차시도

import java.util.*;

class Solution {
    public String[] sortPeople(String[] names, int[] heights) {
        List<String> arrayList = new ArrayList<String>();
        Map<String, Integer> map = new HashMap<>();
        int len = names.length;
        for(int idx = 0; idx < len; idx++) {
            map.put(names[idx], heights[idx]);
        }

        List<Map.Entry<String, Integer>> entryList = new LinkedList<>(map.entrySet());
        entryList.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue() - o1.getValue();
            }
        });

        for(Map.Entry<String, Integer> entry : entryList) {
            arrayList.add(entry.getKey());
        }
        return arrayList.toArray(new String[arrayList.size()]);
        
    }
}

 

class Solution {
    public String[] sortPeople(String[] names, int[] heights) {
        Person[] people = new Person[names.length];

        for (int i = 0; i < names.length; i++) {
            people[i] = new Person(names[i], heights[i]);
        }

        Arrays.sort(people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                if (p1.height == p2.height) {
                    return p1.name.compareTo(p2.name);
                }
                return p2.height - p1.height;
            }
        });

        String[] sortedNames = new String[names.length];

        for (int i = 0; i < names.length; i++) {
            sortedNames[i] = people[i].name;
        }

        return sortedNames;
    }

    class Person {
        String name;
        int height;

        public Person(String name, int height) {
            this.name = name;
            this.height = height;
        }
    }
}

 

키 값을 heights로 map을 구성하여 풀이한 예제

https://leetcode.com/problems/sort-the-people/solutions/2620498/java-hashmap-solution/?languageTags=java

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