My simple Java O(n) without any Data Structure Use


  • 0
    L
         
            
            if (nums == null || k <= 0) {
    			return new int[0];
    		}
             int tempmax=Integer.MIN_VALUE;
    		int temp=Integer.MIN_VALUE;
            
             int output[] = new int[nums.length-k+1];
    		for(int i=0 ;i<nums.length-k+1;i++){
    		  int j=i+k-1;
    		  int m=i;
    		  
    		  System.out.println(i+" " +j);
               
    		  while(m<=j){
    			 temp = nums[m++];
    			 tempmax= Math.max(tempmax, temp);
    		  }
              temp=Integer.MIN_VALUE;  
              output[i] =tempmax;  
              tempmax=Integer.MIN_VALUE;
    		  
    		}
    		return output;
            
            
        } 
         ```

Log in to reply
 

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