[BOJ 11000] ๊ฐ์์ค ๋ฐฐ์
1. ๋ฌธ์ : https://www.acmicpc.net/problem/11000
2. ํ์ด
์ ๋ ฌ + ์ฐ์ ์์ ํ ํฉ์น ๋ฌธ์
๋๋๋ ์๊ฐ์ด ์ ์ ์์๋๋ก peek ํด์ผํ๊ธฐ ๋๋ฌธ์ ์ฐ์ ์์ ํ
* poll๋ง๊ณ peek ์จ๋๋จ...!
3. ์ฝ๋
import java.util.*;
public class Main {
static int N;
static int cnt = 0;
static int arr[][];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
arr = new int[N][2];
for (int i = 0; i < N; i++) {
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] obj1, int[] obj2) {
if(obj1[0] == obj2[0]) return obj1[1] - obj2[1];
else return obj1[0]-obj2[0];
}
});
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(arr[0][1]);
cnt++;
int now;
int next;
int nextEnd;
for(int i=1; i<arr.length; i++){
now = pq.poll();
next = arr[i][0];
nextEnd = arr[i][1];
if(next < now) {
pq.add(now);
cnt++;
}
pq.add(nextEnd);
}
System.out.println(cnt);
}
}
์ ์ฌ๋ฌธ์ - ์ต์ ํ์์ค ๊ฐ์ https://www.acmicpc.net/problem/19598
package baekjoon;
import java.util.*;
public class Main {
static int N;
static int arr[][];
static int cnt;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
arr = new int[N][2];
for(int i=0; i<N; i++){
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] > o2[0]){
return 1;
} else if(o1[0] == o2[0]){
if(o1[1] > o2[1]) return 1;
else return -1;
} else {
return -1;
}
}
});
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(arr[0][1]);
cnt++;
int k=1;
while(true){
if(k>=N) break;
if(pq.peek() > arr[k][0]){
cnt++;
}else{
pq.poll();
}
pq.offer(arr[k][1]);
k++;
}
System.out.println(cnt);
}
}
'๐ป Coding Problems Solving > Stack | Queue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Add Two Numbers (0) | 2023.07.14 |
---|---|
[BOJ 1863] ์ค์นด์ด๋ผ์ธ ์ฌ์ด๊ฑฐ (0) | 2023.06.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๋ฆฐํฐ (์๋ฐ java) (0) | 2023.02.22 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฌ๋ฐ๋ฅธ ๊ดํธ (์๋ฐ java) (0) | 2022.11.25 |
[BOJ 2504] ๊ดํธ์ ๊ฐ (0) | 2022.06.26 |
์ต๊ทผ๋๊ธ