Skip to content

Commit f857325

Browse files
authored
Create maximum-array-hopping-score-i.py
1 parent b6b0bf1 commit f857325

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# prefix sum, greedy
5+
class Solution(object):
6+
def maxScore(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
result = mx = 0
12+
for i in reversed(xrange(1, len(nums))):
13+
mx = max(mx, nums[i])
14+
result += mx
15+
return result
16+
17+
18+
# Time: O(n^2)
19+
# Space: O(n)
20+
# dp
21+
class Solution2(object):
22+
def maxScore(self, nums):
23+
"""
24+
:type nums: List[int]
25+
:rtype: int
26+
"""
27+
dp = [0]*len(nums)
28+
for i in xrange(1, len(nums)):
29+
for j in xrange(i):
30+
dp[i] = max(dp[i], dp[j]+(i-j)*nums[i])
31+
return dp[-1]

0 commit comments

Comments
 (0)