-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3640. Trionic Array II
More file actions
36 lines (29 loc) · 1.01 KB
/
Copy path3640. Trionic Array II
File metadata and controls
36 lines (29 loc) · 1.01 KB
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
class Solution(object):
def maxSumTrionic(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
NEGATIVE_INF = float('-inf')
s0 = nums[0]
s1 = NEGATIVE_INF
s2 = NEGATIVE_INF
s3 = NEGATIVE_INF
max_trionic_sum = NEGATIVE_INF
for i in range(1, len(nums)):
val = nums[i]
prev_val = nums[i-1]
next_s0 = val
next_s1 = NEGATIVE_INF
next_s2 = NEGATIVE_INF
next_s3 = NEGATIVE_INF
if val > prev_val:
next_s0 = max(val, s0 + val)
next_s1 = max(s0, s1) + val
next_s3 = max(s2, s3) + val
elif val < prev_val:
next_s2 = max(s1, s2) + val
s0, s1, s2, s3 = next_s0, next_s1, next_s2, next_s3
if s3 > max_trionic_sum:
max_trionic_sum = s3
return int(max_trionic_sum)