[LeetCode#406] Queue reconstruction by height

2024. 9. 17. 17:26·Algorithm/문제풀이

 

❒ Description


날짜 2024.09.17 (화)
제목 Queue reconstruction by height
링크 https://leetcode.com/problems/queue-reconstruction-by-height/description/
자료구조 ArrayList, PriorityQueue
알고리즘 그리디
시간 복잡도 O(n)

 

이번 문제는 Priority Queue의 순서 설정, ArrayList의 성질 그리고 index 활용이 필요한 문제이다.

 

 

 

 

 

❒ 문제 분석


우선 먼저 우선순위 큐의 정렬 기준을 세워야 한다.

h가 같은 경우 k 기준으로 오름차순 정렬
h가 같지 않을 경우 h 기준으로 내림차순 정렬

 

그리고 k를 index로 활용하여 person을 정렬한다.

 

 

 

 

 

❒ Solution


public int[][] reconstructQueue(int[][] people) {
    Queue<int[]> pq = new PriorityQueue<>(
        (a,b) -> a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]
    );
    
    for (int[] person : people) {
        pq.add(person);
    };
    
    List<int[]> result = new ArrayList<>();
    while (!pq.isEmpty()) {
        int[] person = pq.remove();
        result.add(person[1], person);
    }
    return result.toArray(new int[people.length][2]);
}

 

 

 

 

 

 


'Algorithm > 문제풀이' 카테고리의 다른 글

[LeetCode#134] Gas Station  (0) 2024.09.18
[LeetCode#621] Task Scheduler  (0) 2024.09.17
[LeetCode#122] Best Time to Buy and Sell II  (0) 2024.09.16
[LeetCode#55] Jump Game  (0) 2024.09.16
[LeetCode#743] Network Delay Time  (0) 2024.09.15
'Algorithm/문제풀이' 카테고리의 다른 글
  • [LeetCode#134] Gas Station
  • [LeetCode#621] Task Scheduler
  • [LeetCode#122] Best Time to Buy and Sell II
  • [LeetCode#55] Jump Game
gilbert9172
gilbert9172
gilbert9172 님의 블로그 입니다.
  • gilbert9172
    バックエンド
    gilbert9172
  • 전체
    오늘
    어제
    • All Categories (181)
      • 우테코 7기 (21)
        • 1주차 (8)
        • 2주차 (5)
        • 3주차 (6)
      • Langauge (4)
        • Java (3)
        • Kotlin (1)
      • Back-End (13)
        • SpringBoot (1)
        • Trouble Shooting (0)
        • Setup & Configuration (1)
        • SQL (3)
        • Redis (8)
      • Architecture (6)
        • Multi Module (1)
        • DDD (5)
      • CS (30)
        • Data Structure (6)
        • Operating System (0)
        • Network (12)
        • Database (10)
        • Design Pattern (2)
      • Algorithm (78)
        • 내용 정리 (18)
        • 문제풀이 (60)
      • DevOps (6)
        • AWS (5)
        • Git (1)
      • Front-End (1)
        • Trouble Shooting (1)
      • Project (6)
        • 페이스콕 (6)
      • Book (15)
        • 이벤트 기반 마이크로서비스 구축 (6)
        • 친절한 SQL 튜닝 (5)
        • Spring Batch Docs (4)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    greedy
    오블완
    Back-Tracking
    sliding-window
    Two-Pointer
    부분단조성
    binarysearch
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
gilbert9172
[LeetCode#406] Queue reconstruction by height
상단으로

티스토리툴바