-
Notifications
You must be signed in to change notification settings - Fork 43
/
beautiful-array.py
42 lines (39 loc) · 1.07 KB
/
beautiful-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/83539773
class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
res = [1]
while len(res) < N:
res = [2 * i - 1 for i in res] + [2 * i for i in res]
return [i for i in res if i <= N]
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/83539773
# IDEA : ITERATION
class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
if N == 1: return [1]
odd = [i * 2 - 1 for i in self.beautifulArray(N / 2 + N % 2)]
even = [i * 2 for i in self.beautifulArray(N / 2)]
return odd + even
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
result = [1]
while len(result) < N:
result = [i*2 - 1 for i in result] + [i*2 for i in result]
return [i for i in result if i <= N]