[Solved]/코딩테스트
Least Recently Used
응파카
2023. 3. 14. 23:10
public class Main {
public int[] solution(int c, int n, int[] input) {
int[] ans = new int[c];
for(int i = 0; i < c; i++) {
ans[i] = 0;
}
for(int x : input) {
int idx = -1;
for(int i = 0; i < c; i++) {
if(x == ans[i]) {
idx = i;
}
}
if(idx == -1) {
for(int i = c-1; i >= 1; i--) {
ans[i] = ans[i-1];
}
}
else {
for(int i = idx; i >= 1; i--) {
ans[i] = ans[i-1];
}
}
ans[0] = x;
}
return ans;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Main T = new Main();
Scanner kb = new Scanner(System.in);
int c = kb.nextInt();
int n = kb.nextInt();
int[] input = new int[n];
for(int i = 0; i < n; i++) {
input[i] = kb.nextInt();
}
for(int x : T.solution(c, n, input)) System.out.print(x + " ");
}
}
입력값이
- 캐시 안에 없을 경우 → 전부 오른쪽으로 한 칸씩 땡긴 뒤 앞에 추가
- 캐시 안에 있을 경우 → 현재 index 전까지 오른쪽으로 한 칸씩 땡긴 뒤 맨 앞으로 넣기