ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Programmers

[Java] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํŠœํ”Œ

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 7. 12. 17:39

[๋ฌธ์ œ]

https://school.programmers.co.kr/learn/courses/30/lessons/64065?language=java 

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

[ํ’€์ด]

String ํ˜•์‹์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” ์ง‘ํ•ฉ์˜ ๋ฌถ์Œ s๋ฅผ ๋ฌธ์ž์—ด ํ•จ์ˆ˜๋“ค๋กœ ์ชผ๊ฐœ์–ด ๊ฐ ์ง‘ํ•ฉ์„ ArrayList์— ๋‹ด๊ณ  ๋‚˜์„œ ์ด๋ฅผ ํฌ๊ธฐ ์ˆœ์œผ๋กœ ์ €์žฅ ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ๋ฏผํ•ด๋ณธ ๊ฒฐ๊ณผ, Comparable์„ ์ƒ์†๋ฐ›์€ Class๋ฅผ ์ด์šฉํ•ด ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

Tuple ํด๋ž˜์Šค

Tuple ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜์—ฌ ์ง‘ํ•ฉ์„ ์ €์žฅํ•  set๊ณผ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ size๋ฅผ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค.

compareToํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ๋ฐ›์•„ ํฌ๊ธฐ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•  ์ˆ˜ ์žˆ๊ฒŒ size - o.size๋ฅผ returnํ•ด์ค๋‹ˆ๋‹ค.

 

Tupleํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•œ ํ›„ String s๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ ์ง‘ํ•ฉ๋“ค์„ PriorityQueue<Tuple> pq ์— Tuple ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. pq ์„ธํŒ…์ด ์™„๋ฃŒ๋œ ํ›„ getTuple()๋ฅผ ํ†ตํ•ด poll()์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ฐ ์ง‘ํ•ฉ์˜ ์ˆซ์ž๋ฅผ answer์— ์‚ฝ์ž…ํ•˜๋ฉฐ ์ตœ์ข… ํŠœํ”Œ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

[์ฝ”๋“œ]

import java.util.*;
class Tuple implements Comparable<Tuple>{
    ArrayList<Integer> set;
    int size;
    public Tuple(ArrayList<Integer> set, int size) {
        this.set = set;
        this.size = size;
    }
    @Override
    public int compareTo(Tuple o) {
        return size - o.size;
    }
}
class Solution {
    public static PriorityQueue<Tuple> pq = new PriorityQueue<Tuple>();
    public static int[] answer;
    public int[] solution(String s) {
        String[] str = s.split("");
        int index = 1;
        
        // PQ์— ๊ฐ ์ง‘ํ•ฉ ์›์†Œ๋“ค์„ split์„ ํ†ตํ•ด ์ชผ๊ฐœ์„œ ๋„ฃ์Œ, ์ด๋•Œ ์ง‘ํ•ฉ ํฌ๊ธฐ์ˆœ์œผ๋กœ ์ €์žฅ
        while(index < str.length) {
            if(str[index].equals("{")) {
                int i = index + 1;
                String temp = "";
                ArrayList<Integer> list = new ArrayList<Integer>();
                while(!str[i-1].equals("}")) {
                    if(str[i].equals(",") || str[i].equals("}")) {
                        list.add(Integer.parseInt(temp));
                        temp = "";
                    } else {
                        temp += str[i];   
                    }
                    i++;
                }
                pq.offer(new Tuple(list,list.size()));
            }
            index++;
        }
        getTuple();
        return answer;
    }
    // ํŠœํ”Œ์„ ๊ตฌํ•ด์ค€๋‹ค
    public static void getTuple() {
        ArrayList<Integer> list = new ArrayList<Integer>();
        // ์ž‘์€ List๋ถ€ํ„ฐ ํƒ์ƒ‰
        while(!pq.isEmpty()) {
            Tuple t = pq.poll();
            // ์ด์ „ List์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์ˆซ์ž๋งŒ ์ถ”๊ฐ€
            for(Integer n:t.set) {
                if(!list.contains(n)) {
                    list.add(n);
                }
            }
        }
        answer = new int[list.size()];
        int i = 0;
        for(Integer n:list) {
            answer[i] = n;
            i++;
        }
    }
}