forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-maximum-in-generated-array.py
More file actions
41 lines (38 loc) · 968 Bytes
/
get-maximum-in-generated-array.py
File metadata and controls
41 lines (38 loc) · 968 Bytes
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
# Time: O(n)
# Space: O(n)
nums = [0, 1]
dp = [0, 1]
class Solution(object):
def getMaximumGenerated(self, n):
"""
:type n: int
:rtype: int
"""
if n+1 > len(dp):
for i in xrange(len(nums), n+1):
if i%2 == 0:
nums.append(nums[i//2])
else:
nums.append(nums[i//2] + nums[i//2+1])
dp.append(max(dp[-1], nums[-1]))
return dp[n]
# Time: O(n)
# Space: O(n)
class Solution2(object):
def getMaximumGenerated(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 0
nums = [0]*(n+1)
nums[1] = 1
result = 1
for i in xrange(2, n+1):
if i%2 == 0:
nums[i] = nums[i//2]
else:
nums[i] = nums[i//2] + nums[i//2+1]
result = max(result, nums[i])
return result