9-3. 결혼식 질문있습니다.
234
손홍서
작성한 질문수 1
0
import java.util.*;
import java.io.*;
class Main {
static int N;
static class Time implements Comparable<Time>{
int time;
char state;
Time (int time, char state) {
this.time = time;
this.state = state;
}
public int compareTo(Time t) {
if(time > t.time) {
return 1;
}else if(time == t.time) {
if(state != t.state && t.state == 'e') {
return 1;
}
}
return -1;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
ArrayList<Time> guests = new ArrayList<>();
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
guests.add(new Time(start, 's'));
guests.add(new Time(end, 'e'));
}
Collections.sort(guests);
System.out.println(solution(guests));
}
public static int solution(ArrayList<Time> guests) {
int ans = 0;
int cnt = 0;
for(Time t : guests) {
if(t.state == 's') {
cnt++;
ans = Math.max(ans, cnt);
} else {
cnt--;
}
}
return ans;
}
}
제가 작성한 코드는 위와 같습니다. 강의에서 나온 코드와 거의 흡사한데 채점을 하면 런타임 에러가 발생합니다. 무슨 문제일까요?
감사합니다.
답변 1
0
안녕하세요^^
Time 클래스의 비교 메서드인 compateTo 를 바꾸니까 되네요. 이유는 저도 잘 모르겠습니다.
import java.util.*;
import java.io.*;
class Main {
static int N;
static class Time implements Comparable<Time>{
int time;
char state;
Time (int time, char state) {
this.time = time;
this.state = state;
}
@Override
public int compareTo(Time ob){
if(this.time==ob.time) return this.state-ob.state;
else return this.time-ob.time;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
ArrayList<Time> guests = new ArrayList<>();
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
guests.add(new Time(start, 's'));
guests.add(new Time(end, 'e'));
}
Collections.sort(guests);
System.out.println(solution(guests));
}
public static int solution(ArrayList<Time> guests) {
int ans = 0;
int cnt = 0;
for(Time t : guests) {
if(t.state == 's') {
cnt++;
ans = Math.max(ans, cnt);
} else {
cnt--;
}
}
return ans;
}
}
안녕하세요. 바뀐 채점사이트 관련해서 문의드립니다.
0
28
1
갑자기 채점 사이트가 바뀌었어요
0
32
1
문제 리스트 페이지
0
29
1
채점 사이트 관련 질문드립니다
0
23
1
봉우리 문제 질문입니다
0
81
2
씨름 선수 문제에서 각 선수의 몸무게나 키가 같을 수도 있다면?
0
64
0
이 코드랑 영상 코드중에 뭐가 더 좋은 코드인가요?
0
72
0
가중치 방향 그래프에서 가중치가 0인 간선을 표현하는 방법
0
67
1
좌표 정렬 문제 이 코드가 왜 틀린지 모르겠습니다 ㅠㅠ
0
85
2
6-7 강의에서
0
48
1
6-6. 장난꾸러기 질문 있습니다.
0
45
1
강의 수강후 코딩테스트
0
110
1
answer 변수 사용 여부
0
45
1
2중 for문
1
85
2
2-11. 임시반장정하기 (Runtime Error)
0
63
1
혹시 LinkedList 같은 자료 구조들은 따로 배우지 않나요?
0
70
1
이런 풀이는 어떨까요
0
44
1
자바 스트림 방식의 효율성 질문 드립니다.
0
57
1
알고리즘 자료 구조들..
0
62
1
StringBuilder vs BufferdWriter
0
48
1
원더랜드(프림)
0
50
1
이런 코드는 어떤가요?
0
61
1
bfs 풀이
0
57
1
병합정렬
0
56
1





