일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- spring mongodb
- 자바 백준 팩토리얼 개수
- java 파티
- java 백준 1509
- 익명 객체 @transactional
- 백준 1504 java
- 백준 특정한 최단 경로
- rabbitmq 싱글톤
- kotiln const val
- javav 1676
- Spring ipfs
- ipfs singletone
- 안정해시
- java 팩토리얼 개수
- java 1509
- 백준 연결요소 자바
- 자바 1676
- Java Call By Refernce
- ipfs bean
- mongodb lookup
- kotiln const
- go
- kotiln functional interface
- spring mongodb switch
- spring mongoTemplate switch
- nodejs rabbitmq
- spring mongoTemplate
- java 1238
- 전략 패턴이란
- 백준 2252 줄세우기
Archives
- Today
- Total
공부 흔적남기기
[JAVA] 백준 강의실 1374 본문
728x90
반응형
최근에 코테를 2개정도 봤는데
이런 우선순위 큐를 사용한 그리디 문제를 2개 마주했었다.
시작하는 시간과 끝나는 시간이 주어지고 내부적으로 다양한 조건이 존재하는 문제들이다.
이 문제는 간단하게 시작하는시간 끝나는 시간 그리고 겹칠경우 공간을 +1 해주면 되는 비교적 할만한 문제였다.
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
import java.io.*;
class Main {
static class Lecture{
int start;
int fin;
Lecture(int s, int f){
this.start =s;
this.fin =f;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
PriorityQueue<Lecture> q = new PriorityQueue<>((o1, o2) -> o1.start-o2.start);
for(int i =0; i<n; i++){
String[] s = br.readLine().split(" ");
int number = Integer.parseInt(s[0]);
int start = Integer.parseInt(s[1]);
int fin = Integer.parseInt(s[2]);
q.offer(new Lecture(start, fin));
}
PriorityQueue<Integer> pq = new PriorityQueue<>();
Lecture l = q.poll();
int answer = 1;
pq.offer(l.fin);
while(!q.isEmpty()){
l = q.poll();
int last = pq.peek();
if(last > l.start){
answer++;
}else{
pq.poll();
}
pq.offer(l.fin);
}
bw.write(String.valueOf(answer));
bw.flush();
bw.close();
}
}
728x90
반응형