입력
첫째 줄에 피로연에 참석할 인원수 N(5<=N<=100,000)이 주어집니다.
두 번째 줄부터 N줄에 걸쳐 각 인원의 오는 시간과 가는 시간이 주어집니다.
시간은 첫날 0시를 0으로 해서 마지막날 밤 12시를 72로 하는 타임라인으로 오는 시간과 가는 시간이 음이 아닌 정수로 표현됩니다.
출력
첫째 줄에 피로연장에 동시에 존재하는 최대 인원을 출력하세요.
예시 입력 1
5
14 18
12 15
15 20
20 30
5 14
예시 출력 1
2
import java.util.*;
class Time implements Comparable<Time>{
public int time;
public 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;
}
}
class Main {
public int solution(ArrayList<Time> arr){
int answer=Integer.MIN_VALUE;
Collections.sort(arr);
int cnt=0;
for(Time ob : arr){
if(ob.state=='s') cnt++;
else cnt--;
answer=Math.max(answer, cnt);
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n=kb.nextInt();
ArrayList<Time> arr = new ArrayList<>();
for(int i=0; i<n; i++){
int sT=kb.nextInt();
int eT=kb.nextInt();
arr.add(new Time(sT, 's'));
arr.add(new Time(eT, 'e'));
}
System.out.println(T.solution(arr));
}
}
배열 형태 입출력 학습
첫 번째 줄에 첫 번째 배열의 크기 N(1<=N<=100)이 주어집니다.
두 번째 줄에 N개의 배열 원소가 오름차순으로 주어집니다.
세 번째 줄에 두 번째 배열의 크기 M(1<=M<=100)이 주어집니다.
네 번째 줄에 M개의 배열 원소가 오름차순으로 주어집니다.
각 리스트의 원소는 int형 변수의 크기를 넘지 않습니다.
예시 입력
3
1 3 5
5
2 3 6 7 9
예시 출력
1 2 3 3 5 6 7 9
import java.util.*;
public class Main {
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n=kb.nextInt();
int[] a=new int[n];
for(int i=0; i<n; i++){
a[i]=kb.nextInt();
}
int m=kb.nextInt();
int[] b=new int[m];
for(int i=0; i<m; i++){
b[i]=kb.nextInt();
}
for(int x : T.solution(n, m, a, b)) System.out.print(x+" ");
}
}
첫 줄에 N(5<=N<=100,000)과 K(2<=K<=N)가 주어집니다.
두 번째 줄에 N개의 숫자열이 주어집니다. 각 숫자는 500이하의 음이 아닌 정수입니다.
예시 입력
10 3
12 15 11 20 25 10 20 19 13 15
예시 출력
56
public class Main {
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = kb.nextInt();
}
System.out.print(T.solution(n, k, arr));
}
}
반응형
'CS 기초 > 알고리즘 · 문제풀이' 카테고리의 다른 글
[프로그래머스] K번째수 (0) | 2022.06.29 |
---|---|
[프로그래머스] DESC / ASC (0) | 2022.05.31 |
[JAVA] DFS, BFS 활용 - 동전 교환 (0) | 2022.05.04 |
Dynamic Programming(동적계획법) - 계단오르기 (0) | 2022.03.20 |
Greedy Algorithm - 씨름 선수 (0) | 2022.03.20 |