Hyunebee
최대 강연료는 얼마? 본문
그리디
import java.util.*;
import java.util.stream.Stream;
class Money{
int Money;
int time;
public Money(int money, int time) {
this.Money = money;
this.time = time;
}
public int getMoney() {
return Money;
}
public int getTime() {
return time;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // 배열의 크기
ArrayList<Money> arrayList = new ArrayList<>();
for(int i = 0; i < N; i++){
int height = sc.nextInt();
int weight = sc.nextInt();
arrayList.add(new Money(height, weight));
}
Collections.sort(arrayList, (Money x, Money y) -> y.time - x.time);
Main T = new Main();
System.out.println(T.solution(arrayList));
}
public int solution(ArrayList<Money> arr){
Money max = arr.stream().max((x, y) -> Integer.compare(x.getTime(), y.getTime())).get();
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
int cnt = 0;
int j = 0;// 다시 초기화가 안되게끔
for(int i = max.time; i >=1 ; i--){
for( ; j < arr.size(); j++){
if(arr.get(j).time < i){
break;
}
priorityQueue.offer(arr.get(j).Money);
}
if(!priorityQueue.isEmpty()){
cnt += priorityQueue.poll();
}
}
return cnt;
}
}
'Java > 코테' 카테고리의 다른 글
노드 정렬하기 (0) | 2022.05.26 |
---|---|
연속된 수중 가장 큰값을 리턴하는 부분수열 (0) | 2022.05.26 |
동시에 몇명까지 가능할까? (0) | 2022.05.19 |
회의를 몇번 들어갈수있나? (0) | 2022.05.19 |
씨름선수선발하기 (0) | 2022.05.19 |