Leetcode 968 Solution

This article provides solution to leetcode question 968 (beautiful-array)

https://leetcode.com/problems/beautiful-array

Solution

class Solution:
    def beautifulArray(self, N: int) -> List[int]:
        self.memo = {}

        def get(N):
            if N == 0:
                return []
            elif N == 1:
                return [1]

            if N in self.memo:
                return self.memo[N]
            left_arr = get((N + 1) // 2)
            right_arr = get(N // 2)
            return [2 * x - 1 for x in left_arr] + [2 * x for x in right_arr]

        return get(N)