Java solutions beats 98%


  • 0
    A

    My solution beats 98% java

    public class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
    if(nums.length==0 || null==nums) return nums;

        // resulting array
        int[] result = new int[nums.length-k+1];
        
        //step 1 - form the window
        int i=0, j=0;
        int max = Integer.MIN_VALUE;
        for(;i<k;i++){
            max = Math.max(max, nums[i]);
        }
        
        //step 2 - slide the window
        for(; j<nums.length && i<nums.length ; j++, i++){
            result[j] = max; // put the first value in array
            if(nums[j]==max){
                int tempj = j;
                max = nums[i];
                while(tempj<i){
                    tempj++;
                    max = Math.max(max, nums[tempj]);
                }
            }else{
                max = Math.max(max, nums[i]);
            }
        }
        result[j] = max;
        
        return result;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.