Skip to content

Instantly share code, notes, and snippets.

@jacknie84
Last active April 13, 2018 09:24
Show Gist options
  • Save jacknie84/3f425beb8fa4c1a02d25621339edb362 to your computer and use it in GitHub Desktop.
Save jacknie84/3f425beb8fa4c1a02d25621339edb362 to your computer and use it in GitHub Desktop.
카카오신입공채 코딩테스트 1차(JAVA)
package com.jacknie.doodle.kakao;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.function.Supplier;
public class KakaoTest3 {
public static void main(String[] args) {
int t1 = getRunningTime(3, new String[] {"Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"});
int t2 = getRunningTime(3, new String[] {"Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"});
int t3 = getRunningTime(2, new String[] {"Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"});
int t4 = getRunningTime(5, new String[] {"Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"});
int t5 = getRunningTime(2, new String[] {"Jeju", "Pangyo", "NewYork", "newyork"});
int t6 = getRunningTime(0, new String[] {"Jeju", "Pangyo", "Seoul", "NewYork", "LA"});
System.out.println(Arrays.asList(t1, t2, t3, t4, t5, t6));
}
static int getRunningTime(int cacheSize, String[] cities) {
LruCache cache = new LruCache(cacheSize);
for (String city : cities) {
cache.get(city, Object::new);
}
return cache.getHitCount() + (cache.getMissCount() * 5);
}
static class LruCache {
private final LinkedHashMap<String, Object> map;
private final int cacheSize;
private int hitCount;
private int missCount;
public LruCache(int cacheSize) {
this.map = new LinkedHashMap<>();
this.cacheSize = cacheSize;
}
public Object get(String key, Supplier<Object> contentsSupplier) {
key = key.toUpperCase();
Object value = map.get(key);
if (value == null) {
missCount++;
value = contentsSupplier.get();
map.put(key, value);
while (map.size() > cacheSize) {
String deleteKey = map.keySet().iterator().next();
map.remove(deleteKey);
}
return value;
}
else {
hitCount++;
return value;
}
}
public int getHitCount() {
return hitCount;
}
public int getMissCount() {
return missCount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment