package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/sliding-window-maximum/
 */
public class Problem239 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem239 prob = new Problem239();
        System.out.println(Arrays.toString(prob.maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3))); // [3,3,5,5,6,7]
    }
}

@@ -29,22 +29,52 @@
 va.util.
-Arrays
+Comparator;%0Aimport java.util.TreeMap
 ;%0A%0A/**%0A 
@@ -226,16 +226,36 @@
     
-// TODO%0A
+if (nums.length == 0) %7B%0A    
     
@@ -270,233 +270,742 @@
 rn n
-ull;%0A
+ew int%5B0%5D;%0A  
     
+  
 %7D%0A
-%0A
+ 
     
-public static void main(String%5B%5D args) %7B%0A        Problem239 prob = new Problem239();%0A        System.out.println(Arrays.toString(prob.maxSlidingWindow(new int%5B%5D%7B1, 3, -1, -3, 5, 3, 6, 7%7D, 3))); // %5B3,3,5,5,6,7%5D
+   int%5B%5D answer = new int%5Bnums.length - k + 1%5D;%0A        TreeMap%3CInteger, Integer%3E map = new TreeMap%3C%3E(Comparator.reverseOrder());%0A        int j = 0;%0A        int count = 0;%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            if (count == k) %7B%0A                answer%5Bj%5D = map.firstKey();%0A                int c = map.get(nums%5Bj%5D) - 1;%0A                if (c == 0) %7B%0A                    map.remove(nums%5Bj%5D);%0A                %7D else %7B%0A                    map.put(nums%5Bj%5D, c);%0A                %7D%0A                j++;%0A                count--;%0A            %7D%0A            map.put(nums%5Bi%5D, map.getOrDefault(nums%5Bi%5D, 0) + 1);%0A            count++;%0A        %7D%0A        answer%5Bj%5D = map.firstKey();%0A        return answer;
 %0A   

Solve problem 239