Notice
Recent Posts
Recent Comments
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

I'm pine thank you and you?

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ (Java, PriorityQueue) ๋ณธ๋ฌธ

Algorithm

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ (Java, PriorityQueue)

SollyJ 2023. 6. 27. 20:47

๐Ÿ’œ ๋ฌธ์ œ

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

 

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

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

programmers.co.kr


๐Ÿง PriorityQueue(์šฐ์„ ์ˆœ์œ„ ํ) in Java

์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜

์ผ๋ฐ˜์ ์ธ ํ์ฒ˜๋Ÿผ ์„ ์ž…์„ ์ถœ(FIFO)์ธ๋ฐ, ์—ฌ๊ธฐ์— ์šฐ์„ ์ˆœ์œ„ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

์ฆ‰, ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ฌ๋•Œ๋งˆ๋‹ค ์šฐ์„ ์ˆœ์œ„์— ๋งž๊ฒŒ ์•Œ์•„์„œ ์ •๋ ฌ๋˜๊ณ  ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๊ฒƒ์„ ๋จผ์ € ๋ณด๋‚ด๋Š” ๊ตฌ์กฐ!

๋”ฐ๋ผ์„œ, ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ Comparator ๋˜๋Š” Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ ํ•ด์•ผ ํ•œ๋‹ค. 

Compatator๊ณผ Comparable์˜ ์ฐจ์ด๋Š” ์ด๊ณณ์„ ํด๋ฆญ

(๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ๋ฉด ๋‚ฎ์€์ˆซ์ž ์šฐ์„ ์ˆœ์œ„๊ฐ€ default)

import java.util.PriorityQueue;
import java.util.Collections;

// ๋‚ฎ์€์ˆซ์ž ์šฐ์„ ์ˆœ์œ„
PriorityQueue<Integer> priorityQueueDefault = new PriorityQueue<>();

// ๋†’์€์ˆซ์ž ์šฐ์„ ์ˆœ์œ„
PriorityQueue<Integer> priorityQueueHighest = new PriorityQueue<>(Collections.reverseOrder());

// Comparable์„ ์ด์šฉํ•œ ์‚ฌ์šฉ์ž ์ •์˜ (๋žŒ๋‹ค์‹)
PriorityQueue<Integer> priorityQueueComparable = new PriorityQueue<>((a, b) -> {
	int first = Math.abs(a);
    int second = Math.abs(b);
    
    if(first == second)	return (a > b ? 1 : -1);
 	else	return first - second;
});

// Compatator์„ ์ด์šฉํ•œ ์‚ฌ์šฉ์ž ์ •์˜ 
PriorityQueue<Integer> priorityQueueComparator = new PriorityQueue<>(new Comparator<Student>() {
    @Override
    public int compare(int a, int b) {
       return a - b;	// ์˜ค๋ฆ„์ฐจ์ˆœ
    }
});

๐Ÿ“ ํ’€์ด

  1. ๋ฌธ์ œ ์ด๋ฆ„ ์ฒ˜๋Ÿผ ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ๋‘๊ฐœ ์„ ์–ธํ•˜์—ฌ ํ’€๋ฉด ๋œ๋‹ค.
  2. ๋‚ฎ์€ ์ˆซ์ž ์šฐ์„ ์ˆœ์œ„ํ(pqLow), ๋†’์€ ์ˆซ์ž ์šฐ์„ ์ˆœ์œ„ํ(pqHigh)
  3. ์—ฐ์‚ฐ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค.
  4. ์—ฐ์‚ฐ์ด ๋๋‚œ ํ›„, ์šฐ์„ ์ˆœ์œ„ํ ๋‘˜ ์ค‘์— ํ•˜๋‚˜๋ผ๋„ ๋น„์–ด์žˆ์œผ๋ฉด {0, 0}์„, ์•„๋‹ˆ๋ฉด {์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’}์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋”๋ณด๊ธฐ
	import java.util.Arrays;
	import java.util.Collections;
	import java.util.PriorityQueue;

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

			PriorityQueue pqLow = new PriorityQueue<>();    
			PriorityQueue pqHigh = new PriorityQueue<>(Collections.reverseOrder());    

			for (String operation : operations) {
				String o = operation.split(" ")[0];
				int n = Integer.parseInt(operation.split(" ")[1]);

				if (o.equals("I")) {
					pqLow.add(n);
					pqHigh.add(n);
				} else if (o.equals("D")) {
					if (pqLow.isEmpty() || pqHigh.isEmpty())
						continue;

					if (n < 0) {
						int min = pqLow.poll();
						pqHigh.remove(min);
					} else {
						int max = pqHigh.poll();
						pqLow.remove(max);
					}
				}
			}

			if (pqLow.isEmpty() || pqHigh.isEmpty()) {    // ๋‘˜์ค‘์— ํ•˜๋‚˜๋งŒ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋„ ์ƒ๊ด€์—†์ง€๋งŒ ๋‘˜๋‹ค ์ ์–ด์ฃผ์—ˆ๋‹ค.
				answer = new int[] {0, 0};
			} else {
				answer = new int[] {pqHigh.poll(), pqLow.poll()};
			}

			return answer;
    	}
	}
}