골드1문제가 어느 정도 난이도인지 감을 잡기 위해서 한번 도전해봤다!
같은 티어대 문제중에선 쉬운편 인 것 같다.
역시 원리는 간단하다.
n개 플러그에 꽂을 k개의 수를 입력받고
플러그에 꽂혀있는 수를 저장할 리스트에 초기값으로 중복되지 않은 3개의 값을 넣어준다.
그리고 k개의 수를 반복문을 돌면서 비교하는데 이때 꽂혀있는 플러그를 뽑는 조건은,
1. 이후에 사용할 일이 없을때
2. n개의 플러그 중에 가장 마지막에 사용할때
이렇게 된다.
이 두가지 조건만 구현해주면 간단하게 풀 수 있다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
List<String> arr = Arrays.asList(br.readLine().split(" "));
List<String> flug = new ArrayList<>();
for (String s : arr) {
if (flug.size() == n) {
break;
}
if (!flug.contains(s)) {
flug.add(s);
}
}
int count = 0;
for (int i = n; i < k; i++){
if (!flug.contains(arr.get(i))) {
int max = 0;
int loc = 0;
for (int j = 0; j < flug.size(); j++) {
loc = arr.subList(i, k).indexOf(flug.get(j));
if (loc == -1) {
max = j;
break;
} else {
max = Math.max(loc, max);
}
}
if (loc == -1) {
flug.set(max, arr.get(i));
count++;
} else {
int idx = flug.indexOf(arr.get(max + i));
if (idx != -1) {
flug.set(idx, arr.get(i));
count++;
}
}
}
}
System.out.println(count);
}
}
'코테' 카테고리의 다른 글
[이코테] 꼭 필요한 자료구조 기초 (0) | 2024.04.05 |
---|---|
백준 1195번 : 킥다운 (1) | 2024.01.09 |
백준 1339번 : 단어 수학 (3) | 2023.12.24 |
백준 12904번 : A와 B (0) | 2023.12.23 |
백준 10991번 : 별 찍기 - 16 (0) | 2023.11.24 |