실버 4
https://www.acmicpc.net/problem/1337
package Baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class S4_1337 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(Integer.parseInt(br.readLine()));
}
Collections.sort(list);
int[] cnt = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 1; j < 5; j++) {
if (!list.contains(list.get(i) + j)) {
cnt[i]++;
};
}
}
// 정렬해서 맨처음 값 출력하기
// Arrays.sort(cnt);
// System.out.println(cnt[0]);
int min = cnt[0];
for (int c : cnt) {
if (c <= min) {
min = c;
}
}
System.out.println(min);
}
}
배열의 각 요소마다 1, 2, 3, 4, 5를 더한 값이 입력 리스트에 있는지 비교했다. 없는만큼 개수를 1 더했고, 최종적으로 개수 리스트의 최소값을 출력했다.
효율 생각해서 어렵게 접근하니까 잘 안풀렸다...
감 잡는데 좀 오래걸림...