Sort by

recency

|

338 Discussions

|

  • + 0 comments

    This is a really clear introduction to deques and their usage. I like how it shows both LinkedList and ArrayDeque implementations — it helps beginners see the options available in Java. funinexchange247

  • + 0 comments

    simplest logic

    Scanner in = new Scanner(System.in);
                Deque<Integer> deque = new ArrayDeque<>();
                
                int n = in.nextInt();
                int m = in.nextInt();
                int max = 0;
                for (int i = 0; i < n; i++) {
                    int num = in.nextInt();
                    deque.add(num);
                    if(deque.size()==m){
                        int size = new HashSet<Integer>(deque).size();
                        if(max<size) max = size;
                        deque.removeFirst();
                    }
                }
                in.close();
                System.out.println(max);
    
  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
          Scanner sc=new Scanner(System.in);
          int n=sc.nextInt();
          int m=sc.nextInt();
          Deque<Integer> deq=new ArrayDeque<>();
          Map<Integer,Integer> hm=new HashMap<>();
          int res=0;
          for(int i=0;i<n;i++){
            int ele=sc.nextInt();
            deq.offerLast(ele);
            hm.put(ele,hs.getOrDefault(ele, 0)+1);
            if(deq.size()==m){
                res=Math.max(res, hm.size());
                int out=deq.pollFirst();
                int count=hm.get(out);
                if(count==1){
                    hm.remove(out);
                }else{
                    hm.put(out,hs.get(out)-1);
                }
                
            }
          }
          System.out.println(res);
        }
    }
    
  • + 0 comments

    Set took too much time.

    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            try (BufferedReader bIn = new BufferedReader(new InputStreamReader(System.in))) {
                // get max length;
                String line = bIn.readLine();
                int setLength = Integer.valueOf(line.split(" ")[1]).intValue();
                
                // find max unique number
                line = bIn.readLine();
                Deque<Long> l = new LinkedList<>();
                Map<Long, Integer> m = new HashMap<>();
                int maxUnique = 0;
                
                for (String str : line.split(" ")) {
                    Long val = Long.valueOf(str);
                    l.add(val);
                    
                    if (m.containsKey(val)) {
                        m.put(val, m.get(val) + 1);
                    } else {
                        m.put(val, 1);
                    }
                    
                    if (l.size() == setLength) {
                        maxUnique = Math.max(maxUnique, m.size());
                        Long clearVal = l.poll();
                        int sum = m.get(clearVal) - 1;
                        if (sum == 0) {
                            m.remove(clearVal);
                        } else {
                            m.put(clearVal, sum);
                        }
                    }
                }
                
                System.out.println(maxUnique);
            } catch (Exception e) {
                //
            }
        }
    }
    
  • + 0 comments

    my solution: import java.util.*; public class test { public static void main(String[] args) { Scanner in = new Scanner(System.in); Deque deque = new ArrayDeque<>(); Set set = new HashSet<>(); int n = in.nextInt(); int m = in.nextInt(); int max = 0;

            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.addLast(num);
                set.add(num);
    
            if(deque.size() == m){
                max = Math.max(max, set.size());
                int first = deque.removeFirst();
                if(!deque.contains(first)){
                    set.remove(first);
                }
            }
        }
       System.out.println(max); 
     }
    
    }