백준 5883 - 아이폰 9S
알고리즘/백준

백준 5883 - 아이폰 9S

[문제 바로가기]

 

1. 유형

구현

 

2. 풀이

N이 1000이어서, O(N^2)으로 충분히 풀이 가능.

예제1을 보면 지울 수 있는 숫자 후보는 2,3,5,7 입니다.

위 숫자를 지운 상태에서 N번 탐색하는 식으로 구해주면 됩니다.

 

중복 체크를 해주기 위해 HashSet에 입력값을 넣어요.

이전값과 비교해가면서 가장 긴 숫자를 찾아요.

3. 코드

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int N = stoi(st.nextToken());
		Set<Integer> set = new HashSet<>();
		int arr[] = new int[N];
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(bf.readLine());
			arr[i] = stoi(st.nextToken());
			set.add(arr[i]);
		}
		int ans = 1;
		for(int k: set) {
			//System.out.println(k);
			int cnt=1;
			int pre=arr[0];
			for(int i=1; i<N; i++) {
				if(arr[i]==k) continue;	
				if(pre!=arr[i]) {
					cnt=1;
				}else {
					cnt++;
					ans = Math.max(ans, cnt);
				}
				pre = arr[i];
			}
		}
		System.out.println(ans);
	}
	static int stoi(String s) {
		return Integer.valueOf(s);
	}
}

'알고리즘 > 백준' 카테고리의 다른 글

백준 16987 - 계란으로 계란치기  (0) 2021.11.03
백준 16953 - A -> B  (0) 2021.11.02
백준 17393 - 다이나믹롤러  (0) 2021.10.31
백준 - 좋은 단어  (0) 2021.10.30
백준 21608 - 상어 초등학교  (0) 2021.10.15