Algorithm/BruteForce
[백준][BOJ 5883] - 아이폰 9S
Lazy_developer
2023. 10. 5. 16:40
문제
https://www.acmicpc.net/problem/5883
5883번: 아이폰 9S
사람 9명이 줄을 서있고 각 사람이 원하는 용량의 크기는 2, 7, 3, 7, 7, 3, 7, 5, 7 이다. 용량 3을 원하는 사람을 줄에서 빼버리면, 줄은 2, 7, 7, 7, 7, 5, 7가 되고, 7을 원하는 사람이 4명이 연속된 구간이
www.acmicpc.net
문제 접근
- 특정 용량을 원하는 사람을 줄에서 제거했을 때 같은 용량을 원하는 연속된 사람의 길이의 최대값을 구하는 문제
- 모든 경우의 수를 탐색해야 한다. 예를 들어 0 0 1 2 0 0 3 0 3 3 이라는 입력이 있을 때 제거하는 경우의 수는 각각 0, 1, 2, 3이다. 이 모두를 탐색하면 된다.
- 즉 종류를 한번 카운팅해야 한다는 것이며 이는 Set에다 저장하여 확인할 수 있도록 하였다.
정답 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int ans = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] line = new int[N];
Set<Integer> capacity = new HashSet<>();
for (int i = 0; i < N; i++){
line[i] = Integer.parseInt(br.readLine());
capacity.add(line[i]);
}
for (var removeElement: capacity){
solve(removeElement, line);
}
System.out.println(ans);
}
public static void solve(int element, int[] line){
int cnt = 1;
int pre = line[0];
for (int i = 1; i < line.length; i++){
if (line[i] == element) continue;
if (pre != line[i]){
cnt = 1;
} else{
cnt++;
ans = Math.max(ans, cnt);
}
pre = line[i];
}
}
}