본문 바로가기
JAVA

[PCCP 모의고사 #1] 운영체제

by Son 2023. 1. 12.

https://campus.programmers.co.kr/courses/15436/lessons/132846

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import java.util.*;
class Solution {
    public long[] solution(int[][] program) {
        long[] answer = new long[11]; //정답
        int proLen = program.length;
        Arrays.sort(program, (o1, o2) -> o1[1] - o2[1]); //배열정렬
        PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> { //index0부터 작은것 비교해서 넣는 우선queue
            if(o1[0] == o2[0]) {
                return o1[1] - o2[1];
            }
            return o1[0] - o2[0];
        });
        
        int cnt = 0;
        int lastComp = 0;
        int idx = 0;
        int time = 0;
        while(cnt < proLen) {
            
            for(int i = idx; i < proLen; i++) {
                int sc = program[i][0];  //점수
                int ti = program[i][1];   //호출된 시각
                int ex = program[i][2];   //프로그램 실행시간
                int arr[] = {sc, ti, ex};
                if(ti <= time) {  //현재시간보다 호출된 시각이 작으면
                    pq.add(arr);  //queue에 넣기
                    idx++;
                } else {
                    break;
                }
            }
            
            if(!pq.isEmpty() && lastComp <= time) {
                int ar[] = pq.poll();  //
                int sc = ar[0];
                int ti = ar[1];
                int ex = ar[2];
                
                lastComp = time + ex;  //현재시간 time + 프로그램 실행시간
                answer[sc] += time - ti;  //현재시간 - 프로그램 호출시간 = 대기시간
                cnt++;
            }
            
            
            
            
            time++;
        }
        answer[0] = lastComp;  //프로그램 전체 실행시간
       
        
        return answer;
    }
    
}

1. PriorityQueue<int[]> pq = new PriorityQueue<> PriorityQueue의 개념을 알아햐단다

 

2.배열을 정리할때  1번째 b는 프로그램이 호출된 시각 2.프로그램의 점수 3.프로그램의 실행 시간을 비교하여 배열을 정렬해야한다