biryu2205 / Biryu

0 stars 0 forks source link

Hackerrank Java Dequeue #103

Closed biryu2205 closed 6 years ago

biryu2205 commented 7 years ago
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Scanner;

public class test {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Deque<Integer> deque = new ArrayDeque<>();
    HashSet<Integer> set = new HashSet<>();

    int n = in.nextInt();
    int m = in.nextInt();
    int max = Integer.MIN_VALUE;

    for (int i = 0; i < n; i++) {
      int input = in.nextInt();

      deque.add(input);
      set.add(input);

      if (deque.size() == m) {
        if (set.size() > max) max = set.size();
        int first = deque.remove();
        if (!deque.contains(first)) set.remove(first);
      }
    }

    System.out.println(max);
  }
}

Link -https://www.hackerrank.com/challenges/java-dequeue/problem