๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ/Java - Lv.3

by The Future Engineer, Lucy 2024. 10. 10.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42628

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 16.5 MB, ์‹œ๊ฐ„: 608.55 ms

๋ฌธ์ œ ์„ค๋ช…

์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ๋‹ค์Œ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

๋ช…๋ น์–ด ์ˆ˜์‹  ํƒ‘(๋†’์ด)
I ์ˆซ์ž ํ์— ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
D 1 ํ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
D -1 ํ์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ํ•  ์—ฐ์‚ฐ operations๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋ชจ๋“  ์—ฐ์‚ฐ์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด [0,0] ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด [์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’]์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • operations๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ 1,000,000 ์ดํ•˜์ธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • operations์˜ ์›์†Œ๋Š” ํ๊ฐ€ ์ˆ˜ํ–‰ํ•  ์—ฐ์‚ฐ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ์›์†Œ๋Š” “๋ช…๋ น์–ด ๋ฐ์ดํ„ฐ” ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.- ์ตœ๋Œ“๊ฐ’/์ตœ์†Ÿ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ์—ฐ์‚ฐ์—์„œ ์ตœ๋Œ“๊ฐ’/์ตœ์†Ÿ๊ฐ’์ด ๋‘˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ, ํ•˜๋‚˜๋งŒ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
  • ๋นˆ ํ์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋ผ๋Š” ์—ฐ์‚ฐ์ด ์ฃผ์–ด์งˆ ๊ฒฝ์šฐ, ํ•ด๋‹น ์—ฐ์‚ฐ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

operations return
["I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"] [0,0]
["I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333"] [333, -45]

โœ๐Ÿปํ’€์ด

๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฑ์ฒ˜๋Ÿผ ํ™œ์šฉ.
I์ธ ๊ฒฝ์šฐ ๊ฐ’์„ ์‚ฝ์ž….
D์ด๋ฉด์„œ 1์ธ ๊ฒฝ์šฐ ์ตœ๋Œ“๊ฐ’ ์‚ญ์ œ. -1์ธ ๊ฒฝ์šฐ ์ตœ์†Ÿ๊ฐ’ ์‚ญ์ œ.
๊ฐ’์„ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•œ ํ›„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ. (n๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋จ)
๋น„์–ด์žˆ๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ 0์œผ๋กœ ์ฒ˜๋ฆฌ.
๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’์€ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์œผ๋กœ, ์ตœ์†Ÿ๊ฐ’์€ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌ.

๐Ÿ’ป์ฝ”๋“œ 1

/** ๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ **/
/** ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๊ณ„์† ์ •๋ ฌํ•˜๋ฏ€๋กœ ์†๋„๋ฉด์—์„œ ์ข‹์ง€ ์•Š์Œ **/
import java.util.*;

class Solution {
    public int[] solution(String[] operations) {
        int[] answer = new int[2];

        List<Integer> dq = new ArrayList<>();

        for(int i = 0; i < operations.length; i++){
            String[] s = operations[i].split(" ");
            if(s[0].equals("I")){
                dq.add(Integer.parseInt(s[1]));
            }
            else if(!dq.isEmpty() && s[0].equals("D")){
                if(s[1].equals("1"))
                    dq.remove(0);
                else
                    dq.remove(dq.size() - 1);
            }
            dq.sort((o1, o2) -> (o2 - o1));
        }

        if(dq.isEmpty()){
            answer[0] = 0;
            answer[1] = 0;
        }else{
            answer[0] = dq.get(0);
            answer[1] = dq.get(dq.size() - 1);
        }

        return answer;
    }
}

๐Ÿ’ป์ฝ”๋“œ 2

/** ์šฐ์„ ์ˆœ์œ„ํ ์‚ฌ์šฉ **/
import java.util.*;

class Solution {
    public int[] solution(String[] operations) {
        int[] answer = new int[2];

        PriorityQueue<Integer> pq = new PriorityQueue<>(); // ์ตœ์†Œ ํž™
        PriorityQueue<Integer> rpq = new PriorityQueue<>(Collections.reverseOrder()); // ์ตœ๋Œ€ ํž™

        for(int i = 0; i < operations.length; i++){
            String[] s = operations[i].split(" ");
            if(s[0].equals("I")){
                pq.add(Integer.parseInt(s[1]));
                rpq.add(Integer.parseInt(s[1]));
            }
            else if(!pq.isEmpty() && !rpq.isEmpty() && s[0].equals("D")){
                if(s[1].equals("1")){
                    int max = rpq.poll();
                    pq.remove(max);
                }
                else{
                    int min = pq.poll();
                    rpq.remove(min);
                }
            }
        }

        if(pq.isEmpty() && rpq.isEmpty()){
            answer[0] = 0;
            answer[1] = 0;
        }else{
            answer[0] = rpq.peek();
            answer[1] = pq.peek();
        }

        return answer;
    }
}

๐Ÿ’ก์ƒˆ๋กœ ๋ฐฐ์šด ๋‚ด์šฉ

๊ธฐ๋ณธ์ ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ์‚ญ์ œ ์‹œ ๋ฃจํŠธ ๋…ธ๋“œ ๊ฐ’์„ ์ œ๊ฑฐ.
Java์—์„œ PriorityQueue ํด๋ž˜์Šค์—์„œ removeํ•จ์ˆ˜์— ์ œ๊ฑฐํ•˜๊ณ ์žํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๊ฐ’ ์‚ญ์ œ.

728x90
๋ฐ˜์‘ํ˜•