-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCache.java
50 lines (39 loc) · 1.23 KB
/
Cache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package algo.Algorithms;
import algo.Packing;
import java.util.*;
/**
* https://programmers.co.kr/learn/courses/30/lessons/17680?language=java
* 카카오 2017 기출 ( 캐시 ) 난이도 하
*/
public class Cache {
public static void main(String[] args) {
int cacheSize = 3;
String[] cities = {"Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"};
System.out.println(solution(cacheSize, cities));
}
public static int solution(int cacheSize, String[] cities) {
int answer = 0;
if (cacheSize == 0) {
return cities.length * 5;
}
Queue<String> queue = new LinkedList<>();
for (String city : cities) {
queue.add(city.toLowerCase());
}
Queue<String> cache = new LinkedList<>();
while (!queue.isEmpty()) {
String current = queue.poll();
if (cache.contains(current)) {
answer += 1;
cache.remove(current);
} else {
answer += 5;
if (cache.size() >= cacheSize) {
cache.poll();
}
}
cache.add(current);
}
return answer;
}
}