Python | Find Pattern | with explainations | TC: O(n) SC: O(n)
I find a pattern to construct the list:\n1. Construct a list of 1, 2, ..., n-k\n2. Append(n-k) + k\n3. Append(n-k)+k - (k-1)\n4. Append(n-k)+k - (k-1) + (k-2)\n
I find a pattern to construct the list:\n1. Construct a list of ```1, 2, ..., n-k```\n2. Append```(n-k)``` **+** ```k```\n3. Append```(n-k)+k``` **-** ```(k-1)```\n4. Append```(n-k)+k - (k-1)``` **+** ```(k-2)```\n5. Reapt the above steps until we append ```k``` numbers (the total length of ```res``` is ```n```) \nI personally think this question isn\'t interesting. We can use ```sign``` to determine ```+``` or ```-```. Also you can refer to the example in the comment of the code.\n```\n        n = 8, k = 5\n        1 2 3 8 4 7 5 6\n        1 1 5 4 3 2 1\n```\n```\nclass Solution:\n    def constructArray(self, n: int, k: int) -> List[int]:\n        # n = 8, k = 5\n        # 1 2 3 8 4 7 5 6\n        # 1 1 5 4 3 2 1\n        res = list(range(1,n-k+1))\n        sign, val = 1, k\n        for i in range(k):\n            res.append(res[-1]+sign*val)\n            sign *= -1\n            val -= 1\n        return res\n```
['Python']