[JAVA] front k+1 element will be (1, k+1, 2, k, ...) and remaining is its own index num / T : O(N), S : O(N)


  • 0
    J
    class Solution {
        public int[] constructArray(int n, int k) {
            int[] result = new int[n];
            
            int max = k+1;
            for(int i = 0; i < k+1 ; i++){
                if(i % 2  == 0){
                    result[i] = i/2 + 1;
                }
                else{
                    result[i] = max - i/2;
                }
            }
            
            for(int i = k+1; i < n; i++){
                result[i] = i+1;
            }
            
            return result;
        }
    }
    

Log in to reply
 

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