
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find sum of the minimums of each sublist from a list in Python
Suppose we have a list of numbers called nums. We have to find the sum of minimum of x for every sublist x in nums. If the answer is too large, then mod the result by 10^9 + 7.
So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be 90 because, the sublists are [[5], [10], [20], [10], [0], [5,10], [10,20], [20,10], [10,0], [5,10,20], [10,20,10], [20,10,0], [5,10,20,10], [10,20,10,0], [5,10,20,10,0]], and their minimum values are [5, 10, 20, 10, 0, 5, 10, 10, 0, 5, 10, 0, 5, 0, 0], so the sum is 90.
To solve this, we will follow these steps −
- ans := 0
- s := a new list
- temp_sum := 0
- for each index and value in nums, do
- while s and value <= element at index 1 of last list in s, do
- temp_sum := temp_sum - element at index 2 of last list in s
- delete last element from s
- if s is empty, then
- insert a list with three elements [index, value, (index + 1)*value] in s
- otherwise,
- insert a list with three elements [index, value, (index - first element of the last list of s)*value]
- temp_sum := temp_sum + element at index 2 of last list in s
- ans := ans + temp_sum
- while s and value <= element at index 1 of last list in s, do
- return ans mod (10^9 + 7)
Example
Let us see the following implementation to get better understanding −
def solve(nums): ans = 0 s = [] temp_sum = 0 for index, value in enumerate(nums): while s and value <= s[-1][1]: temp_sum -= s[-1][2] s.pop() if not s: s.append([index, value, (index + 1) * value]) else: s.append([index, value, (index - s[-1][0]) * value]) temp_sum += s[-1][2] ans += temp_sum return ans % (10**9 + 7) nums = [5, 10, 20, 10, 0] print(solve(nums))
Input
[5, 10, 20, 10, 0]
Output
90
- Related Articles
- Python program to find Sum of a sublist
- Program to find the maximum sum of circular sublist in Python
- Program to find the sum of largest K sublist in Python
- Program to find sum of contiguous sublist with maximum sum in Python
- Program to check sublist sum is strictly greater than the total sum of given list Python
- Program to find sum of odd elements from list in Python
- Program to find sum of minimum trees from the list of leaves in python
- Program to find length of longest sublist whose sum is 0 in Python
- Python program to find the group sum till each K in a list
- Java Program to Find a Sublist in a List
- Python program to find Cumulative sum of a list
- Program to find sum of concatenated pairs of all each element in a list in Python?\n
- Program to find size of smallest sublist whose sum at least target in Python
- Sum of Subarray Minimums in C++
- Python program to find sum of elements in list

Advertisements