What's wrong with my Code?


  • 0
    X

    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.ListIterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;

    public class LFUCache {

    private Map<String, Node> cache = null;
    private int capacity = 0;
    private int used = 0;
    
    public LFUCache(int capacity) {
    	cache = new LinkedHashMap<>(capacity);
    	this.capacity = capacity;
    }
    
    public int get(int key) {
    	Node node = cache.get(String.valueOf(key));
    	if (node == null) {
    		return -1;
    	}
    	node.useCount++;
    	node.lastGetTime = System.nanoTime();
    	cache.put(String.valueOf(key), node);
    	System.out.println(node.value + ":" + node.useCount + ":" + node.lastGetTime);
    	return node.value;
    }
    
    public void set(int key, int value) {
    	used++;
    	if (cache.get(String.valueOf(key)) != null) {
    		cache.remove(String.valueOf(key));
    		used--;
    	}
    	Node node = new Node();
    	node.value = value;
    	node.useCount = 0;
    	if (used <= this.capacity) {
    		cache.put(String.valueOf(key), node);
    	} else {
    		removeLast();
    		if (cache.size() < this.capacity) {
    			cache.put(String.valueOf(key), node);
    		}
    	}
    
    }
    
    // remove  
    private void removeLast() {
    	int minCount = 0;
    	long getTime = 0;
    	int flag = 0;
    	String waitRemoveKey = null;
    	Set<Entry<String, Node>> set = this.cache.entrySet();
    	ArrayList<Entry<String, Node>> list = new ArrayList<>(set);
    	ListIterator<Entry<String, Node>> itera = list.listIterator();
    	while (itera.hasNext()) {
    		Map.Entry<String, Node> entry = itera.next();
    		flag++;
    		String key = entry.getKey();
    		Integer count = entry.getValue().useCount;
    		long lastGetTime = entry.getValue().lastGetTime;
    		if (flag == 1) {
    			minCount = count;
    			waitRemoveKey = key;
    			getTime = entry.getValue().lastGetTime;
    			if (minCount == 0) {
    				break;
    			}
    		}
    		if (count < minCount ) {
    			minCount = count;
    			waitRemoveKey = key;
    			getTime =lastGetTime;
    		}
    		if (minCount == count) {
    			if (getTime > lastGetTime) {
    				minCount = count;
    				waitRemoveKey = key;
    				getTime = lastGetTime;
    			}
    		}
    	}
    	
    	if (waitRemoveKey != null) {
    		this.cache.remove(waitRemoveKey);
    	}
    
    }
    
    public class Node {
    
    	public int value;
    	public int useCount;
    	public long lastGetTime;
    	
    }
    
    public static void main(String[] args) {
    	LFUCache cache = new LFUCache(2);
    	cache.set(1, 1);
    	cache.set(2, 2);
    	cache.set(3, 3);
    	System.out.println(cache.get(3));
    	System.out.println(cache.get(2));
    	cache.set(4, 1);
    	System.out.println(cache.get(3));
    	System.out.println(cache.get(4));
    	System.out.println(cache.get(2));
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.