
- 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 minimum sum subsequence by taking at least one element from consecutive 3 elements in python
Suppose we have a a list of numbers called nums, we have to find a minimum sum subsequence from the given list such that at least one number for all groups of three consecutive numbers is selected. If the length of given list is less than 3, a number should still be selected.
So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 7, as we can select 2 and 5.
To solve this, we will follow these steps:
- n := size of nums
- if n is same as 0, then
- return 0
- if n is same as 1, then
- return nums[0]
- if n is same as 2, then
- return minimum of nums[0] and nums[1]
- table := a list of size n and fill with 0
- table[0] := nums[0]
- table[1] := nums[1]
- table[2] := nums[2]
- for i in range 3 to n, do
- table[i] := nums[i] + minimum of table[i - 3], table[i - 2] and table[i - 1]
- res := minimum of table[n - 1], table[n - 2] and table[n - 3]
- return res
Let us see the following implementation to get better understanding:
Example Code
class Solution: def solve(self, nums): n = len(nums) if n == 0: return 0 if n == 1: return nums[0] if n == 2: return min(nums[0], nums[1]) table = [0] * n table[0] = nums[0] table[1] = nums[1] table[2] = nums[2] for i in range(3, n): table[i] = nums[i] + min(table[i - 3], table[i - 2], table[i - 1]) res = min(table[n - 1], table[n - 2], table[n - 3]) return res ob = Solution() nums = [2, 3, 4, 5, 6, 7] print(ob.solve(nums))
Input
[2, 3, 4, 5, 6, 7]
Output
7
- Related Articles
- Maximum sum subsequence with at-least k distant elements in C++ program
- Maximum sum subsequence with at-least k distant elements in C++
- Find Array Elements Which has at Least One Smaller Element in Java
- Program to find elements from list which have occurred at least k times in Python
- Program to find closest subsequence sum in Python
- Find minimum sum such that one of every three consecutive elements is taken in C++
- Count elements that are divisible by at-least one element in another array in C++
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Python program to check if two lists have at least one common element
- Program to find minimum element addition needed to get target sum in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Program to find size of smallest sublist whose sum at least target in Python
- Program to find k where k elements have value at least k in Python
- Program to find minimum elements to add to form a given sum in Python
- Program to find sum of odd elements from list in Python

Advertisements