File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Time: O(n)
2
+ # Space: O(1)
3
+
4
+ # prefix sum, math
5
+ class Solution (object ):
6
+ def maximumSumScore (self , nums ):
7
+ """
8
+ :type nums: List[int]
9
+ :rtype: int
10
+ """
11
+ prefix = suffix = 0
12
+ result = float ("-inf" )
13
+ right = len (nums )- 1
14
+ for left in xrange (len (nums )):
15
+ prefix += nums [left ]
16
+ suffix += nums [right ]
17
+ right -= 1
18
+ result = max (result , prefix , suffix )
19
+ return result
20
+
21
+
22
+ # Time: O(n)
23
+ # Space: O(1)
24
+ # prefix sum
25
+ class Solution2 (object ):
26
+ def maximumSumScore (self , nums ):
27
+ """
28
+ :type nums: List[int]
29
+ :rtype: int
30
+ """
31
+ total = sum (nums )
32
+ prefix = 0
33
+ result = float ("-inf" )
34
+ for x in nums :
35
+ prefix += x
36
+ result = max (result , prefix , total - prefix + x )
37
+ return result
You can’t perform that action at this time.
0 commit comments