667. Beautiful Arrangement II
Last updated
Last updated
/**
* Time complexity : O(N)
* Space complexity : O(N)
*/
class Solution {
public int[] constructArray(int n, int k) {
int[] result = new int[n];
int low = 1, high = n;
int index = 0;
result[index++] = low++;
boolean isHigh = false;
while(k > 1) {
result[index++] = high--;
k--;
isHigh = true;
if(k > 1) {
result[index++] = low++;
k--;
isHigh = false;
}
}
while(index < n) {
if(isHigh) {
result[index++] = high--;
} else {
result[index++] = low++;
}
}
return result;
}
}