Algorithm/Baekjoon
[Java] ๋ฐฑ์ค 1931๋ฒ - ํ์์ค ๋ฐฐ์
๊ฐ๋ฐ๊ฐ๊ตด๐ธ
2022. 6. 13. 21:07
https://www.acmicpc.net/problem/1931
[๋ฌธ์ ]
[ํ์ด]
์ฒ์์ "์์์๊ฐ"๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ, ๊ฐ๋ฅํ ๋ชจ๋ ํ์ ์กฐํฉ์ ๊ตฌํ์ฌ max๊ฐ์ ๋ฝ์๋ด๋ ค ํ์ผ๋, ์๊ฐ์ด๊ณผ๊ฐ ๋์์ต๋๋ค.
๋ฐ๋ผ์ ์ง๋ฌธํ๊ธฐ์ ๋์์ ๋ฐ์ "์ข ๋ฃ์๊ฐ"๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ ํธ๋ ๋ฌธ์ ๋ผ๋ ํํธ๋ฅผ ์ป์์ต๋๋ค.
- Compatable<>์ ์ด์ฉํ์ฌ ์ข ๋ฃ์๊ฐ์ผ๋ก ์ ๋ ฌํ๋ฉฐ, **๋ง์ฝ ์ข ๋ฃ์๊ฐ์ด ๊ฐ๋ค๋ฉด start์๊ฐ์ด ์งง์์์๋๋ก ์ ๋ ฌ
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14
(1,4) -> time: 4 ์ด๋ฏ๋ก 4๋ณด๋ค ์์์๊ฐ์ค ํฌ๊ฑฐ๋ ๊ฐ์๊ฐ์ด ๋์ค๋ฉด ๊ฐฑ์ -> (5,7) -> (8,11) -> (12,14)
** end ์๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ง ๋ชปํด 88%์์ ํ๋ ค์ ์ง๋ฌธํ๊ธฐ์ ์ฌ๋ผ์จ ๋ฐ๋ก๋ฅผ ํตํด ๋ฐ๊ฒฌํ์์ต๋๋ค.
๋ฐ๋ก
5
6 7
6 6
5 6
5 5
7 7
์ ๋ต : 5
์ถ๋ ฅ : 4
[์ฝ๋]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
class Room implements Comparable<Room>{
int start;
int end;
public Room(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public int compareTo(Room o) {
if(this.end == o.end) {
return this.start - o.start;
}
return this.end - o.end;
}
}
public class Main {
public static int N;
public static Room[] room;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
room = new Room[N];
for(int i=0;i<N;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
room[i] = new Room(start, end);
}
Arrays.sort(room);
countRoom();
}
// cnt : ์งํ๋ ํ์ ์ดํฉ
public static void countRoom() {
// ์์์ ๋ฌด์ ๊ฑด 0 ๋ถํฐ์
int time = room[0].end;
int cnt = 1;
for(int i=1;i<N;i++) {
if(time <= room[i].start) {
time = room[i].end;
cnt++;
}
}
System.out.println(cnt);
}
}