1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| import java.util.LinkedList;
import java.util.List;
class Solution {
public int solution(int cacheSize, String[] cities) {
int answer = 0;
if(cacheSize == 0) return cities.length * 5;
List<String> cache = new LinkedList<>();
for(String city : cities) {
String lowcity = city.toLowerCase();
if(cache.contains(lowcity)) {
cache.remove(0);
cache.add(lowcity);
answer += 1;
} else {
if (cache.size() >= cacheSize) cache.remove(0);
cache.add(lowcity);
answer += 5;
}
}
return answer;
}
}
|