https://school.programmers.co.kr/learn/courses/20847/lessons/255903
์๋ฃ๊ตฌ์กฐ
์ฐ์ ์์ ํ ๋ ๊ฐ๋ฅผ ํ์ฉํด์ผ ํฉ๋๋ค.
๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ์ง ์์ ํ๋ก๊ทธ๋จ์ ์ํ ์ฐ์ ์์ ํ, ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ ํ๋ก๊ทธ๋จ์ ์ํ ์ฐ์ ์์ ํ
์ฐ์ ์์๋ฅผ ๊ฒฐ์ ํ๋ ๊ธฐ์ค์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๋๋ค.
- ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ์ง ์์ ํ๋ก๊ทธ๋จ์ ์ํ ์ฐ์ ์์ ํ์ ๊ธฐ์ค : ํ๋ก๊ทธ๋จ ํธ์ถ ์๊ฐ
- ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ ํ๋ก๊ทธ๋จ์ ์ํ ์ฐ์ ์์ ํ์ ๊ธฐ์ค : ํ๋ก๊ทธ๋จ ์ฐ์ ์์ + ํ๋ก๊ทธ๋จ ํธ์ถ์๊ฐ
class Program{
int score, callTime, runTime;
public Program(int score,int callTime, int runTime){
this.score = score;
this.callTime = callTime;
this.runTime = runTime;
}
}
PriorityQueue<Program> left = new PriorityQueue<>((p1,p2)->(p1.callTime-p2.callTime));
PriorityQueue<Program> load = new PriorityQueue<>((p1,p2)->{
if(p1.score==p2.score) return (p1.callTime-p2.callTime);
return (p1.score-p2.score);
});
ํ์ด๊ณผ์
1. ํ์ฌ์๊ฐ(time)์ ๊ณ์ ์ฆ๊ฐ์ํค๋ฉด์ left ํ๋ฅผ loadํ๋ก ๋ค ์ฎ๊ฒจ์ฃผ๋ฉด์ ํ์ฌ์๊ฐ(time)์ loadํ์์ ์คํ์ํฌ ์ ์๋ ํ๋ก๊ทธ๋จ ์คํ์ํค๊ธฐ
์ฐ์ ํ์ฌ ์๊ฐ(time)์ ๊ธฐ์ค์ผ๋ก load ํ๋ก ์ฎ๊ธธ ์ ์๋ ํ๋ก๊ทธ๋จ๋ค์ ๋ค loadํ๋ก ์ฎ๊ฒจ์ค๋๋ค.
๊ทธ๋ฐ ๋ค์ ํ์ฌ ์๊ฐ(time)์์ ์คํ์ํฌ ์ ์๋ ํ๋ก๊ทธ๋จ์ ์คํ์ํต๋๋ค. ๊ทธ๋ฆฌ๊ณ time ์ ํ๋ก๊ทธ๋จ์ ์ํ์๊ฐ ๋งํผ๊ฐฑ์ ํฉ๋๋ค.
๋ง์ฝ ์คํ์ํฌ ํ๋ก๊ทธ๋จ์ด ์๋ค๋ฉด time์ 1 ์ฆ๊ฐ ์ํต๋๋ค.
// time์ ๊ณ์ ์ฆ๊ฐ์ํค๋ฉด์ leftํ๋ฅผ load ํ๋ก ๋ค ์ฎ๊ธฐ๋ ์์
while(true){
while(!left.isEmpty() && left.peek().callTime <= time){ // ํ์ฌ ์๊ฐ(time)์ ๊ธฐ์ค์ผ๋ก left ์์ load ํ๋ก ์ฎ๊ธฐ๊ธฐ
load.offer(left.poll());
}
if(!load.isEmpty() && load.peek().callTime <= time){ // ํ์ฌ ์๊ฐ(time)์ ์คํํ ์์
์ด ์๋ ๊ฒฝ์ฐ (time์ runTime๋งํผ ์ฆ๊ฐ)
Program cur = load.poll();
answer[cur.score] += (time - cur.callTime);
time += cur.runTime;
}else{ // ํ์ฌ ์๊ฐ(time)์ ์คํํ ์์
์ด ์๋ ๊ฒฝ์ฐ (time์ 1์ฆ๊ฐ)
time += 1;
}
if(left.isEmpty()) break; // left ๊ฐ ๋ค ๋น์์ง ์ํ๋ฉด ๋ฐ๋ณต๋ฌธ ์ข
๋ฃ
}
2. load ํ์ ๋จ์์๋ ํ๋ก๊ทธ๋จ ์ํ
// load ํ์ ๋จ์ ์๋ ์์
์ ์ํํ๋ ์ฝ๋
while(!load.isEmpty()){
Program cur = load.poll();
answer[cur.score] += (time - cur.callTime);
time += cur.runTime;
}
์๊ฒ๋ ์
- ์ฐ์ ์์ ํ ๋ ๊ฐ๋ฅผ ๋๊ณ , ๋ ํ ์ฌ์ด์ ๊ฐ์ ์ ๋ฌํ๋ฉด์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์์ ์๊ฒ๋์์ต๋๋ค.
- ๋ ํ๋ฅผ ๋ค ๋น์ฐ๋ ์๊ธฐ๊ฐ ๋ค๋ฅด๋ฏ๋ก, ํ ํ๊ฐ ๋ค ๋น์์ง ๋ค์ ๋ค๋ฅธ ํ๋ฅผ ๋น์์ฃผ๋ ์์ ์ ํด์ค์ผ ํจ์ ์๊ฒ๋์์ต๋๋ค.
- ๊ฐ๋จํด ๋ณด์๋๋ฐ ์ฐ์ ์์ ํ๋ฅผ ๋ ๊ฐ ํ์ฉํ๋ค๋ ์ ๊ณผ, ๋ ์ฐ์ ์์ํ๊ฐ ๋น์์ง๋ ์๊ธฐ๊ฐ ๋ฌ๋ผ์ ๊ตฌํํ๊ธฐ ๊น๋ค๋ก์ ๊ณ , ๊ฒฐ๊ตญ ๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํด์ผ ํ์ต๋๋ค.
์ ์ฒด์ฝ๋
import java.util.*;
class Program{
int score, callTime, runTime;
public Program(int score,int callTime, int runTime){
this.score = score;
this.callTime = callTime;
this.runTime = runTime;
}
}
class Solution {
public long[] solution(int[][] program) {
long[] answer = new long[11];
long time = 0l;
PriorityQueue<Program> left = new PriorityQueue<>((p1,p2)->(p1.callTime-p2.callTime));
PriorityQueue<Program> load = new PriorityQueue<>((p1,p2)->{
if(p1.score==p2.score) return (p1.callTime-p2.callTime);
return (p1.score-p2.score);
});
for(int i=0;i<program.length;i++){
left.offer(new Program(program[i][0],program[i][1],program[i][2]));
}
// time์ ๊ณ์ ์ฆ๊ฐ์ํค๋ฉด์ leftํ๋ฅผ load ํ๋ก ๋ค ์ฎ๊ธฐ๋ ์์
while(true){
while(!left.isEmpty() && left.peek().callTime <= time){ // ํ์ฌ ์๊ฐ(time)์ ๊ธฐ์ค์ผ๋ก left ์์ load ํ๋ก ์ฎ๊ธฐ๊ธฐ
load.offer(left.poll());
}
if(!load.isEmpty() && load.peek().callTime <= time){ // ํ์ฌ ์๊ฐ(time)์ ์คํํ ์์
์ด ์๋ ๊ฒฝ์ฐ (time์ runTime๋งํผ ์ฆ๊ฐ)
Program cur = load.poll();
answer[cur.score] += (time - cur.callTime);
time += cur.runTime;
}else{ // ํ์ฌ ์๊ฐ(time)์ ์คํํ ์์
์ด ์๋ ๊ฒฝ์ฐ (time์ 1์ฆ๊ฐ)
time += 1;
}
if(left.isEmpty()) break; // left ๊ฐ ๋ค ๋น์์ง ์ํ๋ฉด ๋ฐ๋ณต๋ฌธ ์ข
๋ฃ
}
// load ํ์ ๋จ์ ์๋ ์์
์ ์ํํ๋ ์ฝ๋
while(!load.isEmpty()){
Program cur = load.poll();
answer[cur.score] += (time - cur.callTime);
time += cur.runTime;
}
answer[0] = time;
return answer;
}
}
'๐์ฝ๋ฉํ ์คํธ:CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[PCCP ๋ชจ์๊ณ ์ฌ #2] 3๋ฒ - ์นดํ ํ์ฅ (1) | 2024.12.18 |
---|---|
PCCP ๋ชจ์๊ณ ์ฌ1ํ 3๋ฒ (0) | 2024.12.11 |
๋ฉ๋์ฌ์ ์ ์ฌ๋ค (0) | 2024.12.04 |
2024 ์ผ์ฑSW์ญ๋ํ ์คํธ ํ๋ฐ๊ธฐ ์ค์ 1๋ฒ (0) | 2024.12.02 |
์์ด(n๊ณผm ๋ฌธ์ ) ์ต์ ํ ์ํค๊ธฐ && ์กฐํฉ (0) | 2024.10.29 |