Find Sum of Beauty Values of a List

Given an array of integers, find the sum of beauty values of a the items in the list. The beauty values for each index element is defined by certain conditions. The description of which I would defer to the original source in leetcode at this link .

Solution: Solution

class Solution: def sumOfBeauties(self, nums: List[int]) -> int: n = len(nums) min_on_right = [float('inf')]*n max_on_left = [-float('inf')]*n # lets populate max_on_left # .. idx i on this array holds max value in nums in range[0..i] for i in range(1,n): max_on_left[i] = max(max_on_left[i-1],nums[i-1]) for i in range(n-2,-1,-1): min_on_right[i] = min(min_on_right[i+1],nums[i+1]) ret = 0 for i in range(1,n-1): if nums[i] > max_on_left[i] and nums[i] < min_on_right[i]: ret += 2 elif nums[i-1] < nums[i] and nums[i] < nums[i+1]: ret += 1 return ret