Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active August 29, 2024 04:00
Show Gist options
  • Save sandipchitale/031cd099330ad8eaace4f142289f4348 to your computer and use it in GitHub Desktop.
Save sandipchitale/031cd099330ad8eaace4f142289f4348 to your computer and use it in GitHub Desktop.
Kaitenzushi #facebook
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