You are viewing a single comment's thread. Return to all comments →
My attempt:
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Deque<Integer> deque = new LinkedList<>(); int n = sc.nextInt(); int m = sc.nextInt(); int maxUniqueNumbers = 0; Map<Integer, Integer> frequencyMap = new HashMap<>(n); for (int i = 0; i < n; i++) { int currentInt = sc.nextInt(); deque.addLast(currentInt); frequencyMap.compute(currentInt, (k, v) -> Objects.isNull(v) ? 1 : v+1); if (deque.size() != m) continue; if (frequencyMap.size() > maxUniqueNumbers) maxUniqueNumbers = frequencyMap.size(); frequencyMap.compute(deque.pollFirst(), (k, v) -> v <= 1 ? null : v-1); } System.out.println(maxUniqueNumbers); } }
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 attempt: