- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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]))

[4, 4, 6, 4, 4]

17

- Related Questions & Answers
- 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
- Program to Find Out the Probability of Having n or Fewer Points 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 Maximum Final Power of a List in Python
- Program to find out the cells containing maximum value in a matrix in Python
- Program to find out the sum of the maximum subarray after a operation in Python
- Program to find out if k monitoring stations are enough to monitor particular points in Python
- Program to find out the maximum value of a 'valid' array in Python
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to find maximum score from removing stones in Python
- Find maximum points which can be obtained by deleting elements from array in Python
- Program to find out the vertical area between two points where no point lies and is the widest in Python

Advertisements