You are viewing a single comment's thread. Return to all comments →
My version:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numberElements = scanner.nextInt(); int numberSubgroups = scanner.nextInt(); Deque<Integer> elementsWindow = new ArrayDeque<>(numberSubgroups); int maxUniqueValues = 1; HashMap<Integer, Integer> frecuencia = new HashMap<>(numberElements); for (int i = 0; i < numberElements; i++) { int element = scanner.nextInt(); elementsWindow.addLast(element); frecuencia.put(element, frecuencia.getOrDefault(element, 0) + 1); if (elementsWindow.size() == numberSubgroups) { int numInFrecuencia = frecuencia.size(); maxUniqueValues = Math.max(maxUniqueValues, numInFrecuencia); Integer elementRemoved = elementsWindow.removeFirst(); int updatedCount = frecuencia.get(elementRemoved) - 1; if (updatedCount == 0) { frecuencia.remove(elementRemoved); } else { frecuencia.put(elementRemoved, updatedCount); } } } System.out.println(maxUniqueValues); }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Dequeue
You are viewing a single comment's thread. Return to all comments →
My version: