Skip to content

Instantly share code, notes, and snippets.

@ManduTheCat
Created August 3, 2023 11:11
Show Gist options
  • Save ManduTheCat/807dcf2314c87df3b76274b26650b667 to your computer and use it in GitHub Desktop.
Save ManduTheCat/807dcf2314c87df3b76274b26650b667 to your computer and use it in GitHub Desktop.
살려줘..
import java.util.*;
import java.io.*;
class Lacture implements Comparable<Lacture>{
long start;
long end;
Lacture(long start, long end){
this.start= start;
this.end = end;
}
@Override
public int compareTo(Lacture o){
if(o.start == this.start){
return Long.compare(this.end, o.end); //같으면 end가 빠른거
}
return Long.compare(this.start, o.start); // 가장먼저 start 가 빠른거
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(this.start);
sb.append(", ");
sb.append(this.end);
sb.append("]");
return sb.toString();
}
}
public class Main{
static int N;
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
Stack<Lacture> stack = new Stack<>();
List<Lacture> sortedRecture = new ArrayList<>();
for(int n = 0; n < N; n++){
StringTokenizer st = new StringTokenizer(br.readLine());
long start = Long.parseLong(st.nextToken());
long end = Long.parseLong(st.nextToken());
sortedRecture.add(new Lacture(start, end));
}
Collections.sort(sortedRecture);
//System.out.println(sortedRecture);
for(Lacture cur: sortedRecture){// 가능한것끼리 최대한 이어주면된다.
if(stack.isEmpty()){
stack.add(cur);// 비어있으면 스텍에 넣어라
}else if(stack.peek().end <= cur.start){
stack.add(cur); // 만약 다음에 시작할수 있으면 스텍에 넣어라
}//스텍이 있고 다음에 시작이 불가능 하면 넘거어간다.
}
System.out.println(stack);
System.out.println(stack.size());
}
}
@yeojinj
Copy link

yeojinj commented Aug 3, 2023

반례

input
11
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14

output
3

answer
4

@ManduTheCat
Copy link
Author

감사합니다 선생님

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment