문제: https://school.programmers.co.kr/learn/courses/30/lessons/17680
시간을 두고 총 3번 문제를 풀었습니다.
효율성 면에서는 3회차 > 1회차 > 2회차 가 가장 좋았으며
삽입과 삭제 면에서 뛰어난 LinkedList를 실제로 사용해볼 수 있어 좋았습니다..!
/*
1. 없다 -> q 개수 x / q 개수 o
2. 있다 ->
*/
import java.util.*;
class Solution {
public int solution(int cacheSize, String[] cities) {
Queue<String> q = new ArrayDeque<>();
int answer = 0;
if (cacheSize == 0) return cities.length * 5;
for(int i = 0; i < cities.length; i++){
String c = cities[i].toLowerCase();
if(!q.contains(c)){
if(q.size() < cacheSize){
q.offer(c);
}else{
q.poll();
q.offer(c);
}
answer += 5;
}else{
if(q.size() < cacheSize){
int len = q.size();
for(int j = 0; j < len; j++){
String cur = q.poll();
if(cur.equals(c)) continue;
q.offer(cur);
}
q.offer(c);
}else{
for(int j = 0; j < cacheSize; j++){
String cur = q.poll();
if(cur.equals(c)) continue;
q.offer(cur);
}
q.offer(c);
}
answer += 1;
}
}
return answer;
}
}
import java.util.*;
// Queue, ArrayList -> queue안에 있는지 확인
class Solution {
public int solution(int cacheSize, String[] cities) {
ArrayList<String> list = new ArrayList<>();
ArrayDeque<String> queue = new ArrayDeque<>();
int playTime = 0;
if(cacheSize == 0 ) return cities.length * 5;
for(String city : cities ){
city = city.toLowerCase();
if(list.size() < cacheSize){
// 캐시가 차지 않았을 때
if(list.contains(city)){
// 캐시에 있을 경우
ArrayDeque<String> stack = new ArrayDeque<>();
while(!queue.peekFirst().equals(city)){
stack.push(queue.poll());
}
queue.poll();
while(!stack.isEmpty()){
queue.addFirst(stack.pop());
}
queue.offer(city);
playTime += 1;
}else{
// 캐시에 없을 경우
queue.offer(city);
list.add(city);
playTime += 5;
}
}else if(list.size() >= cacheSize){
// 캐시 가득 찬 경우
// 캐시 내 있는 경우
if(list.contains(city)){
ArrayDeque<String> stack = new ArrayDeque<>();
while(!queue.peekFirst().equals(city)){
stack.push(queue.poll());
}
queue.poll();
while(!stack.isEmpty()){
queue.addFirst(stack.pop());
}
queue.offer(city);
playTime += 1;
}else{
// 캐시 내 없는 경우
String deleteCity = queue.poll();
list.remove(deleteCity);
queue.offer(city);
list.add(city);
playTime += 5;
}
}
}
return playTime;
}
}

LinkedList 1개
중간 요소 삭제 면에서 가장 뛰어난 속도를 보여줌
import java.util.*;
class Solution {
public int solution(int cacheSize, String[] cities) {
LinkedList<String> list = new LinkedList<>();
int playTime = 0;
if(cacheSize == 0) return cities.length * 5;
for(String city : cities){
city = city.toLowerCase();
if(list.remove(city)){
playTime += 1;
}else{
if(cacheSize == list.size()) list.removeFirst();
playTime += 5;
}
list.addLast(city);
}
return playTime;
}
}
시간을 두고 총 3번 문제를 풀었습니다.
효율성 면에서는 3회차 > 1회차 > 2회차 가 가장 좋았으며
삽입과 삭제 면에서 뛰어난 LinkedList를 실제로 사용해볼 수 있어 좋았습니다..!