We will find a way, we always have.

-interstellar

Problem Solving/๋ฆฌํŠธ์ฝ”๋“œ

[LeetCode] 406. Queue Reconstruction by Height

Redddy 2024. 11. 8. 23:33

๐Ÿ”ˆ ๋ฌธ์ œ

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [$h_i$, $k_i$] represents the $i^{th}$ person of height $h_i$ with exactly $k_i$ other people in front who have a height greater than or equal to $h_i$.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [$h_j$, $k_j$] is the attributes of the $j^{th}$ person in the queue (queue[0] is the person at the front of the queue). 

 

๐Ÿ’ซ ์˜ˆ์‹œ

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]

 

๐ŸŽฑ ์ œํ•œ

 

  • 1 <= people.length <= 2000
  • 0 <= $h_i$ <= $10^6$
  • 0 <= $k_i$ < people.length

 

๐Ÿง ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐ ํ’€์ด

 

์‚ฌ๋žŒ๋“ค์„ ์ผ๋ ฌ๋กœ ์ค„์„ ์„ธ์› ์„ ๋•Œ, ๋‚ด ํ‚ค์™€ ๋‚ด ์•ž์— ์žˆ๋Š” ์‚ฌ๋žŒ ์ค‘ ๋‚ด ํ‚ค๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์‚ฌ๋žŒ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Ÿฌํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์žฌ๊ตฌ์„ฑํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์ •๋ ฌ์ด๋‹ค. 

 

์—๋ฅผ ๋“ค์–ด [[7,2], [7,0], [7,1]] ์ด ์˜ˆ์ œ๋กœ ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ [[7,0], [7,1], [7,2]] ๋กœ ์ •๋ ฌํ•ด์•ผ ์˜ฌ๋ฐ”๋ฅธ ๋‹ต์ด ๋œ๋‹ค. $k_i$๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ข€ ๋” ๋šซ์–ด์ ธ๋ผ ์ณ๋‹ค๋ณด๋ฉด $k_i$๊ฐ€ ํ˜„์žฌ ์ž์‹ ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋Š” ๊ฑธ ๋ฐœ๊ฒฌ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์—ฌ๊ธฐ์— [6,3] ์ด ์ถ”๊ฐ€ ๋œ [[7,2], [7,0], [7,1], [6,3]] ์„ ์‚ดํŽด๋ณด์ž. ์ด ๊ฒฝ์šฐ์—๋Š” [[7,0], [7,1], [7,2], [6,3]] ์ด ์ •๋‹ต์œผ๋กœ ์—ฌ์ „ํžˆ $k_i$๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋‹ค. 

 

๊ทธ๋ ‡๋‹ค๋ฉด [6,3] ์ด ์•„๋‹Œ [6,1] ์„ ์˜ˆ์‹œ๋กœ ๋“ค์–ด๋ณด์ž. [[7,2], [7,0], [7,1], [6,1]] ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, [[7,0], [6,1], [7,1], [7,2]] ์ด ์ •๋‹ต์ด ๋œ๋‹ค. [6,1] ์—์„œ $k$๋Š” ํ˜„์žฌ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์ง€๋งŒ [7,1]์€ ๋”์ด์ƒ ์ž์‹ ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿผ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ‹€๋ฆฐ๊ฒƒ์ผ๊นŒ...?

 

๊ทธ๋ ‡์ง€ ์•Š๋‹ค. [7,1]๋„ [6,1] ์ด ์˜ค๊ธฐ ์ „๊นŒ์ง€๋Š” $k$๊ฐ€ ์ž์‹ ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์—ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด [6,1] ์ด ์ถ”๊ฐ€๋˜์ž ์ด์ „์— ๊ฒƒ๋“ค์˜ ์ธ๋ฑ์Šค๋Š” ๋ฌด์‹œ๋˜๊ณ , ํ˜„์žฌ ์ถ”๊ฐ€๋˜๋Š” ๊ฐ’์˜ $k$๊ฐ€ ๋” ์ค‘์š”ํ•ด์กŒ๋‹ค. ํ‚ค๊ฐ€ ํฐ ์ˆœ์œผ๋กœ ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ดํ›„์— ์ถ”๊ฐ€๋˜๋Š” ๊ฐ’๋“ค์€ ์–ด์ฐจํ”ผ ๋‚˜๋ณด๋‹ค ํ‚ค๊ฐ€ ์ž‘์„ ํ…Œ๋‹ˆ ์•ž์— ์˜ค๋“  ๋’ค์— ์˜ค๋“  ์ƒ๊ด€์ด ์—†๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ‚ค๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” $k$ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ ์–ด๋””์— ์ถ”๊ฐ€ํ•ด์•ผ ํ• ๊นŒ? ๋ฐ”๋กœ $k$, ์ฆ‰ ์ธ๋ฑ์Šค์— ํ•˜๋ฉด๋œ๋‹ค.

 

์ •๋ฆฌํ•˜์ž๋ฉด, ํ‚ค ๋‚ด๋ฆผ์ฐจ์ˆœ, ๋‚ด ํ‚ค๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆœ์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ๊ฐ ์›์†Œ๋Š” ์ •๋‹ต ๋ฐฐ์—ด ์ธ๋ฑ์Šค์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๐Ÿ’ป ์ฝ”๋“œ

 

Python

 

1
2
3
4
5
6
7
class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        result = []
        for h, k in sorted(people, key=lambda x: (-x[0], x[1])):
            result.insert(k, [h, k])
        return result
 
cs

 

 

Java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (a1, a2) -> {
            if (a1[0!= a2[0]) {
                return a2[0- a1[0];
            }
            return a1[1]-a2[1];
        });
        final List<int[]> res = new LinkedList<>();
        final int n = people.length;
        for (final int[] person : people) {
            res.add(person[1], person);
        }
        return res.toArray(new int[n][2]);
    }
}
 
cs

 

 

Rust

 

1
2
3
4
5
6
7
8
9
10
11
impl Solution {
    pub fn reconstruct_queue(mut people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
        people.sort_by_key(|x| (-x[0], x[1]));
        let mut res = Vec::with_capacity(people.len());
        for person in people.iter() {
            res.insert(person[1] as usize, person.to_vec());
        }
        res
    }
}
 
cs

 

 

์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ •๋ ฌํ•  ๋•Œ $O(n * log n)$, ๊ฐ’ ์ถ”๊ฐ€ํ•  ๋•Œ ์ตœ์•…์˜ ๊ฒฝ์šฐ $O(n)$ ์ด๋ฅผ $n$ ๋ฒˆ ๋ฐ˜๋ณต์ด๋‹ˆ๊นŒ ์ด $O(n^2)$ ์ด ๋‚˜์˜จ๋‹ค. 

 

์ž๋ฐ”๋กœ ๋ฌธ์ œ ํ’€ ๋•Œ ArrayList ๋ง๊ณ  LinkedList ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ๋น ๋ฅด๋ ค๋‚˜? ํ•˜๊ณ  ๋ฐ”๊ฟ”๋ณด์•˜๋‹ค. ArrayList ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ’ ์ฐพ๋Š”๋ฐ $O(1)$ ๊ทธ ๊ฐ’ ์ดํ›„์— ๊ฐ’๋“ค ์ „๋ถ€ ํ•œ์นธ์”ฉ ๋’ค๋กœ ๋ฏธ๋Š” ์ž‘์—…์ด $O(n)$์ด๊ณ , LinkedList ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ’ ์ฐพ๋Š”๋ฐ $O(n)$ ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๋…ธ๋“œ ์ฃผ์†Œ ๋ณ€๊ฒฝํ•˜๋Š”๋ฐ $O(1)$ ์ด๋ฏ€๋กœ ๋‘˜ ๋‹ค $O(n)$ ์ด์ง€๋งŒ, ์ „๋ถ€ ํ•œ์นธ์‹ ๋’ค๋กœ ๋ฏธ๋Š” ์ž‘์—…์ด ๊ฐ’ ์ฐพ๋Š” ์ž‘์—…๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™์•˜๋‹ค. ํ•˜์ง€๋งŒ ์œ ์˜๋ฏธํ•œ ์ฐจ์ด๋Š” ์—†์—ˆ๋‹ค. 

 

๊ทธ๋ž˜๋„ ์—ฌ๊ธฐ์„œ ๋ฆฌ์Šค์ฝ”ํ”„ ์น˜ํ™˜์›์น™์˜ ๋ง›์„ ๋ดค๋‹ค. ArryaList๋กœ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒƒ์„ LinkedList๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ”๊พผ ์ฝ”๋“œ๋Š” ๋‹จ ํ•œ์ค„, ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•  ๋•Œ์˜€๋‹ค. ps ํ•˜๋ฉด์„œ ๊ฐ์ฒด์ง€ํ–ฅ ๋Š๊ปด๋ณด๋Š”๊ฑด ๊ฑฐ์˜ ์ฒ˜์Œ์ด์—ˆ๋‹ค. 

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ