Simple python solution


  • 0
    Y

    The idea is to append(1, 1 + k, 2, k, 3, k - 1...), since the difference is k, k-1, k-2...1, then append the remaining numbers in order

    class Solution:
        def constructArray(self, n, k):
            """
            :type n: int
            :type k: int
            :rtype: List[int]
            """
            ret = [1]
            v = k
            while v:
                if len(ret) % 2:
                    ret.append(ret[-1] + v)
                else:
                    ret.append(ret[-1] - v)
                v -= 1
            ret.extend(range(2 + k, n + 1))
            return ret
    

Log in to reply
 

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