Java solution beats 100%


  • 2

    my first beats of 100%, amazing....although dont konw why...code is not succinct anyway

    public class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            if(k==0)return new int[0];
            if(k==1)return nums;
            int[] result=new int[nums.length-k+1];
            int max=-2147483648,j=0;
            for(int i=0;i<k;i++){
                if(nums[i]>max)max=nums[i];
            }
            result[j++]=max;
            for(int i=k;i<nums.length;i++){
                if(nums[i]>=max){
                    max=nums[i];
                    result[j++]=max;
                }
                else if(max!=nums[i-k]){
                    result[j++]=max;
                }
                else{
                    max=-2147483648;
                    for(int m=i-k+1;m<=i;m++){
                        if(nums[m]>max)max=nums[m];
                    }
                    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.