https://ilmiodiario.tistory.com/106
[프로그래머스] level2. [1차] 캐시 (자바 JAVA)
[ 문제 ] [프로그래머스] level2. [1차] 캐시 (자바 JAVA) 문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/17680 코딩테스트 연습 - [1차] 캐시 3 ["Jeju", "Pangyo", "Seoul", "NewYork", "L..
ilmiodiario.tistory.com
https://nack1400.tistory.com/94
[Algorithm/Java] 알고리즘 자바 LRU (코딩테스트, Sorting, Searching, 정렬, 검색, Least Recently Used)
1. 문제 설명 설명 캐시메모리는 CPU와 주기억장치(DRAM) 사이의 고속의 임시 메모리로서 CPU가 처리할 작업을 저장해 놓았다가 필요할 바로 사용해서 처리속도를 높이는 장치이다. 워낙 비싸고 용
nack1400.tistory.com
※. LRU알고리즘
※. remove(object) : class값을 넣는다면 그 값을 삭제 후 true를 반환, 값이 없다면 false를 반환
※. cacheSize가 0일 때를 생각해준다.
import java.util.LinkedList;
class Solution {
public int solution(int cacheSize, String[] cities) {
if(cacheSize == 0) {
return cities.length * 5;
}
int answer = 0;
LinkedList<String> que = new LinkedList<>();
for(int i = 0; i < cities.length; i++) {
String str = cities[i].toLowerCase();
if(que.remove(str)) {
answer += 1;
que.add(str);
}
else {
answer += 5;
if(que.size() == cacheSize) {
que.poll();
}
que.add(str);
}
}
return answer;
}
}
'프로그래머스' 카테고리의 다른 글
[프로그래머스 Level 2] 튜플 [Java] (2) | 2022.09.30 |
---|---|
[프로그래머스 Level 2] H-Index [Java] (0) | 2022.09.29 |
[프로그래머스 Level 2] 멀리 뛰기 [Java] (0) | 2022.09.24 |
[프로그래머스 Level 2] 짝지어 제거하기 [Java] (0) | 2022.09.24 |
[프로그래머스 Level 1] 체육복 [Python] (0) | 2022.04.22 |