본문 바로가기

Algorithm (Java)/인프런 알고리즘강의

[인프런/알고리즘] 최대수입스케쥴 (우선순위 큐)

 
 

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    static int n, max=Integer.Min_Value;
    
    public int solution(ArrayList<Lecture> list){
        int answer =0;
        PriorityQueue<Integer> pQ = new PriorityQueue(Collections.reverseOrder());
        Collections.sort(list);

        int j=0; 
        for(int i=max; i>=1; i--){
            for(int ; j<n ;j++){
                if(list.get(j).date<i){
                    break;
                }
                pQ.offer(list.get(j).money);
            }
            if(!pq.isEmpty()){
                answer += pQ.poll();  // 최댓값이 나옴
            }
        }
        return answer;
    }

    static ArrayList<MoneyDay> list = new ArrayList<>();
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        for(int i=0; i<n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int m = Integer.parseInt(st.nextToken());
            int d = Integer.parseInt(st.nextToken());
            list.add(new MoneyDay(m,d));
            if(d>max) max=d;

        }

        solution(list);


    }

public class MoneyDay{


}


public class Lecture implements Comparable<Lecture>{
    int money, date;
    public MoneyDay(int money, int date){
        this.money= money;
        this.date = date;

    }

    @Override 
    public int compareTo(Lecture o){
        return o.date -this.date; // 1. time기준 내림차순

    }


}


출처
https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4-%EC%BD%94%ED%85%8C%EB%8C%80%EB%B9%84


자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비 - 인프런 | 강의

www.inflearn.com