Skip to content

Commit 5c087f7

Browse files
authored
Create sum-of-beauty-in-the-array.py
1 parent bbeafa1 commit 5c087f7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/sum-of-beauty-in-the-array.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def sumOfBeauties(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
right = [nums[-1]]*len(nums)
11+
for i in reversed(xrange(2, len(nums)-1)):
12+
right[i] = min(right[i+1], nums[i])
13+
result, left = 0, nums[0]
14+
for i in xrange(1, len(nums)-1):
15+
if left < nums[i] < right[i+1]:
16+
result += 2
17+
elif nums[i-1] < nums[i] < nums[i+1]:
18+
result += 1
19+
left = max(left, nums[i])
20+
return result

0 commit comments

Comments
 (0)