forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-sum-of-mountain-triplets-ii.py
51 lines (46 loc) · 1.31 KB
/
minimum-sum-of-mountain-triplets-ii.py
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
42
43
44
45
46
47
48
49
50
51
# Time: O(n)
# Space: O(n)
# prefix sum
class Solution(object):
def minimumSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
INF = float("inf")
right = [INF]*len(nums)
curr = INF
for i in reversed(xrange(len(nums))):
right[i] = curr
curr = min(curr, nums[i])
result = curr = INF
for i in xrange(len(nums)):
if curr < nums[i] > right[i]:
result = min(result, curr+nums[i]+right[i])
curr = min(curr, nums[i])
return result if result != INF else -1
# Time: O(n)
# Space: O(n)
# prefix sum
class Solution2(object):
def minimumSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
INF = float("inf")
left = [INF]*len(nums)
curr = INF
for i in xrange(len(nums)):
left[i] = curr
curr = min(curr, nums[i])
right = [INF]*len(nums)
curr = INF
for i in reversed(xrange(len(nums))):
right[i] = curr
curr = min(curr, nums[i])
result = INF
for i in xrange(len(nums)):
if left[i] < nums[i] > right[i]:
result = min(result, left[i]+nums[i]+right[i])
return result if result != INF else -1