package leetcode;

/**
 * https://leetcode.com/problems/beautiful-array/
 */
public class Problem932 {
    public int[] beautifulArray(int N) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem932 prob = new Problem932();
        System.out.println(prob.beautifulArray(1)); // [1]
        System.out.println(prob.beautifulArray(2)); // [1,2]
        System.out.println(prob.beautifulArray(3)); // [1,3,2]
        System.out.println(prob.beautifulArray(4)); // [2,1,4,3]
        System.out.println(prob.beautifulArray(5)); // [3,1,2,5,4]
        System.out.println(prob.beautifulArray(6)); // [1,5,3,2,6,4]
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -198,85 +198,162 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args
+Map%3CInteger, int%5B%5D%3E memo = new HashMap%3C%3E();%0A        return beautifulArray(N, memo);%0A    %7D%0A%0A    public int%5B%5D beautifulArray(int n, Map%3CInteger, int%5B%5D%3E memo
 ) %7B%0A
@@ -364,427 +364,492 @@
     
-Problem932 prob = new Problem932();%0A        System.out.println(prob.beautifulArray(1)); // %5B1%5D%0A        System.out.println(prob.beautifulArray(2)); // %5B1,2%5D%0A        System.out.println(prob.beautifulArray(3)); // %5B1,3,2%5D%0A        System.out.println(prob.beautifulArray(4)); // %5B2,1,4,3%5D%0A        System.out.println(prob.beautifulArray(5)); // %5B3,1,2,5,4%5D%0A        System.out.println(prob.beautifulArray(6)); // %5B1,5,3,2,6,4%5D
+if (memo.containsKey(n)) %7B%0A            return memo.get(n);%0A        %7D%0A        int%5B%5D result = new int%5Bn%5D;%0A        if (n == 1) %7B%0A            result%5B0%5D = 1;%0A        %7D else %7B%0A            int i = 0;%0A            for (int x : beautifulArray((n + 1) / 2)) %7B  // odds%0A                result%5Bi++%5D = 2 * x - 1;%0A            %7D%0A            for (int x : beautifulArray(n / 2)) %7B // evens%0A                result%5Bi++%5D = 2 * x;%0A            %7D%0A        %7D%0A        memo.put(n, result);%0A        return result;
 %0A   

Solve problem 932