Skip to content
LC-0667 Medium LeetCode

667. Beautiful Arrangement II

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 60% Topics: Array, Math
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def constructArray(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[int]
        """
        result = []
        left, right = 1, n
        while left <= right:
            if k % 2:
                result.append(left)
                left += 1
            else:
                result.append(right)
                right -= 1
            if k > 1:
                k -= 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT