We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b6b0bf1 commit f857325Copy full SHA for f857325
Python/maximum-array-hopping-score-i.py
@@ -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
23
24
25
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