You are viewing a single comment's thread. Return to all comments →
import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); Deque<Integer> deque = new LinkedList<>(); Set<Integer> set = new HashSet<>(); int maxUnique = 0; for (int i = 0; i < n; i++) { int num = scanner.nextInt(); deque.add(num); set.add(num); if (deque.size() == m) { maxUnique = Math.max(maxUnique, set.size()); int first = deque.removeFirst(); if (!deque.contains(first)) { set.remove(first); } } } scanner.close(); System.out.println(maxUnique); } }
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 →