ffinn92 / Keep-at-solve-it

꾸준히 알고리즘 풀기 위한 스터디 저장소입니다.
2 stars 3 forks source link

[220816][반스][인프런](9-4) 최대 수입 스케줄 #125

Open ffinn92 opened 2 years ago

ffinn92 commented 2 years ago

📌 문제

⭐️ 아이디어

🤔 고민한 내용

💪 새롭게 배운 내용

🆘 이해가 어려운 내용

❌ 해결하지 못한 이유

✅ 본인 풀이

🏋️‍♀️ 시도횟수 : 1회 | ⏱ 걸린시간 : 516ms | 💾 메모리 : 35MB

import java.util.*;

class Lecture implements Comparable<Lecture> {
    public int income;
    public int days;

    public Lecture(int income, int days) {
        this.income = income;
        this.days = days;
    }
    @Override
    public int compareTo(Lecture ob) {
        return ob.days - this.days;
    }
}

public class If904MaxIncome {
    public int solution (ArrayList<Lecture> arr) {
        int answer = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        Collections.sort(arr);
        int number = 0;
        number = arr.get(0).days;

        for (Lecture lecture : arr) {
            if(lecture.days == number) {
                pq.offer(lecture.income);
                number = lecture.days;
            }else {
                Integer maxIncome = pq.poll();
                answer += maxIncome;
                pq.offer(lecture.income);
                number = lecture.days;
            }
        }

        Integer lastMaxIncome = pq.poll();
        answer += lastMaxIncome;

        return answer;
    }

    public static void main(String[] args) {
        If904MaxIncome T = new If904MaxIncome();
        Scanner sc = new Scanner(System.in);

        int lecturs = sc.nextInt();
        ArrayList<Lecture> arr = new ArrayList<>();
        for (int i = 0; i < lecturs; i++) {
            int income = sc.nextInt();
            int days = sc.nextInt();
            arr.add(new Lecture(income, days));
        }

        System.out.println(T.solution(arr));
    }

}

참고한 자료