Easy to understand JAVA O(N)


  • 0
    M

    First loop for the unique differences...

    Second loop for the remaining numbers with difference 1.

    class Solution {
        public int[] constructArray(int n, int k) {
            int[] out=new int[n];
            out[0]=1;
            boolean up=true;
            k++;
            int c=k+1;
            int i=1;
            while(--k>0){
                if(up) out[i]=out[i-1]+k;
                else out[i]=out[i-1]-k;
                i++;
                up=!up;
            }
            while(c<=n){
                out[i++]=c++;
            }
            return out;
        }
    }
    

Log in to reply
 

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