Program to Find Out the Maximum Points From Removals in Python

In this problem, we need to find the maximum points from removing contiguous sublists of identical values. When we remove a sublist of length t with the same value, we get t * t points. The key insight is that we can strategically remove elements to merge identical values and maximize our score.

Problem Explanation

Given a list like [4, 4, 6, 4, 4], we can:

  • First remove the single 6 ? get 1 * 1 = 1 point
  • Then remove all four 4s together ? get 4 * 4 = 16 points
  • Total: 1 + 16 = 17 points

Dynamic Programming Approach

We use a recursive function dp(left, right, t) where:

  • left, right: boundaries of the current subarray
  • t: number of identical elements we can merge with the leftmost element
class Solution:
    def solve(self, nums):
        def dp(left, right, t):
            if left > right:
                return 0
            
            # Find all consecutive identical elements from left
            num = nums[left]
            left2 = left
            while left2 < right and nums[left2 + 1] == num:
                left2 += 1
            
            # Add the count of identical elements
            t += left2 - left + 1
            left = left2 + 1
            
            # Option 1: Remove all identical elements now
            points = t ** 2 + dp(left, right, 0)
            
            # Option 2: Try to merge with future identical elements
            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)

# Test the solution
solution = Solution()
nums = [4, 4, 6, 4, 4]
result = solution.solve(nums)
print(f"Maximum points for {nums}: {result}")
Maximum points for [4, 4, 6, 4, 4]: 17

How It Works

The algorithm considers two strategies at each step:

  1. Immediate removal: Remove all consecutive identical elements from the left and continue with the rest
  2. Delayed removal: Skip some elements to merge with identical values later, potentially getting more points

Example Walkthrough

# Let's trace through [4, 4, 6, 4, 4]
solution = Solution()

# Test with different examples
test_cases = [
    [4, 4, 6, 4, 4],
    [1, 3, 2, 2, 2, 3, 4, 3, 1],
    [1, 1, 1]
]

for nums in test_cases:
    result = solution.solve(nums)
    print(f"Input: {nums}")
    print(f"Maximum points: {result}")
    print()
Input: [4, 4, 6, 4, 4]
Maximum points: 17

Input: [1, 3, 2, 2, 2, 3, 4, 3, 1]
Maximum points: 23

Input: [1, 1, 1]
Maximum points: 9

Key Points

  • The algorithm uses dynamic programming with memoization for optimal substructure
  • At each position, we decide whether to remove elements immediately or wait to merge with future identical elements
  • Time complexity is O(n³) due to the nested loops and recursive calls
  • The strategy maximizes points by creating larger groups of identical elements

Conclusion

This dynamic programming solution finds the maximum points by exploring both immediate removal and strategic merging of identical elements. The key insight is balancing immediate gains against potential future merges for higher scores.

Updated on: 2026-03-25T13:41:56+05:30

314 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements