fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8.  
  9. int[] data = new int[] {2, 3, 4, 2, 6, 2, 5, 1};
  10. int W = 3;
  11. TreeMap<Integer,Integer> counts = new TreeMap<Integer,Integer>();
  12. for (int i = 0 ; i != W ; i++) {
  13. if (counts.containsKey(data[i])) {
  14. counts.put(data[i], counts.get(data[i])+1);
  15. } else {
  16. counts.put(data[i], 1);
  17. }
  18. }
  19. for (int i = W ; i != data.length ; i++) {
  20. Integer max = counts.lastKey();
  21. System.out.println(max);
  22. int tmp = counts.get(data[i-W])-1;
  23. if (tmp != 0) {
  24. counts.put(data[i-W], tmp);
  25. } else {
  26. counts.remove(data[i-W]);
  27. }
  28. if (counts.containsKey(data[i])) {
  29. counts.put(data[i], counts.get(data[i])+1);
  30. } else {
  31. counts.put(data[i], 1);
  32. }
  33. }
  34. System.out.println(counts.lastKey());
  35.  
  36. }
  37. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
4
4
6
6
6
5