You are viewing a single comment's thread. Return to all comments →
My solution.
public static void main(String[] args) { Scanner in = new Scanner(System.in); Deque<Integer> deque = new ArrayDeque<>(); Map<Integer, Integer> numCount = new HashMap<>(); int max = 0; int n = in.nextInt(); int m = in.nextInt(); for (int i = 0; i < n; i++) { int num = in.nextInt(); deque.offerLast(num); numCount.merge(num, 1, Integer::sum); if (deque.size() > m) { Integer val = deque.pollFirst(); if (numCount.merge(val, -1, Integer::sum) == 0) { numCount.remove(val); } } if (deque.size() == m && numCount.size() > max) { max = numCount.size(); } } in.close(); System.out.println(max); }
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Java Dequeue
You are viewing a single comment's thread. Return to all comments →
My solution.