
- 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 Out the Maximum Points From Removals in Python
Suppose we are provided with a list of positive numbers. Now, here we can remove any contiguous sub list of some length t with the same value and get points t * t. One condition is to be considered, that we can perform this any number of times until the list is empty. So we have to determine the maximum number of points we can get.
So, if the input is like nums = [4, 4, 6, 4, 4], then the output will be 17.
For the output, we can first remove the 6 which has length 1 and yields 1 * 1 = 1 point. Then we can take the list [4, 4, 4, 4] which has length 4 and yields 4 * 4 = 16 points. So finally, we can get 17 points.
To solve this, we will follow these steps −
Define a function dp() . This will take left, right, t
if left > right is non−zero, then
return 0
num := nums[left]
left2 := left
while left2 < right and nums[left2 + 1] is same as num, do
left2 := left2 + 1
t := t + left2 − left + 1
left := left2 + 1
points := t to the power 2 + dp(left, right, 0)
for mid in range left to right + 1, do
if nums[mid] is same as num, then
points := maximum of (points, dp(left, mid − 1, 0) + dp(mid, right, t))
return points
From the main function, we do the following −
print(dp(0, size of nums − 1, 0))
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): def dp(left, right, t): if left > right: return 0 num = nums[left] left2 = left while left2 < right and nums[left2 + 1] == num: left2 += 1 t += left2 − left + 1 left = left2 + 1 points = t ** 2 + dp(left, right, 0) for mid in range(left, right + 1): if nums[mid] == num: points = max(points, dp(left, mid − 1, 0) + dp(mid, right, t)) return points return dp(0, len(nums) − 1, 0) ob1 = Solution() print(ob1.solve([4, 4, 6, 4, 4]))
Input
[4, 4, 6, 4, 4]
Output
17
- Related Articles
- Program to find out the maximum points collectable in a game in Python
- Program to Find Out the Points Achievable in a Contest in Python
- C++ program to find out the maximum possible tally from given integers
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find out is a point is reachable from the current position through given points in Python
- Program to find out the cells containing maximum value in a matrix in Python
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Program to find out the sum of the maximum subarray after a operation in Python
- Program to find out the maximum value of a 'valid' array in Python
- Program to find maximum score from removing stones in Python
- C++ program to find out the maximum value of i
- C++ Program to find out the maximum rated parts set
- Minimum removals from array to make GCD Greater in Python
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to find least number of unique integers after K removals using Python
