Last active
August 29, 2024 04:00
-
-
Save sandipchitale/031cd099330ad8eaace4f142289f4348 to your computer and use it in GitHub Desktop.
Kaitenzushi #facebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int getMaximumEatenDishCount(int N, int[] D, int K) { | |
// N must be less than D.length right? | |
if (N > D.length) { | |
throw new IllegalArgumentException("N must be less than or equal to D.length"); | |
} | |
int count = 0; | |
// LRU cache to store the last K dishes | |
Map<Integer, Integer> lastK = new LinkedHashMap<Integer, Integer>(K, 0.75f, false) { | |
@Override | |
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) { | |
return size() > K; | |
} | |
}; | |
for(int i = 0; i < N; i++) { | |
if (lastK.containsKey(D[i])) { | |
continue; | |
} | |
count++; | |
lastK.put(D[i], 1); | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment