# 5 Best Ways to Find Maximum Additive Score by Deleting Numbers in Python

Rate this post

π‘ Problem Formulation: The challenge involves writing a Python program that maximizes an additive score by deleting elements from a list. For each number, when deleted, it adds to the score the product of the nearest remaining neighbors. Given an input list of integers, the goal is to delete the elements sequentially to maximize the total score. For example, given `[1, 2, 3]`, deleting 2 first gives a score of 3, then delete 1 or 3 for an additional score of 3, resulting in a maximum total score of 6.

## Method 1: Brute Force Recursive Solution

This method explores all possible sequences of deletions exhaustively using recursion to ensure no possible deletions that might lead to a higher score are missed. It’s the most straightforward approach, but potentially the least efficient for large lists.

Here’s an example:

```def max_score(nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
max_score = 0
for i in range(len(nums)):
score = (nums[i-1] if i else 1) * (nums[i+1] if i < len(nums) - 1 else 1)
max_score = max(max_score, score + max_score(nums[:i] + nums[i+1:]))
return max_score

print(max_score([1, 2, 3]))```

Output:

`6`

This function `max_score` recursively computes the score for all possible deletion combinations, updating the maximum score found at each step. The current number’s score is calculated by multiplying its neighbors, or 1 if it is at the beginning/end of the list. The list is then updated by deleting the current number, and the function calls itself until all numbers are deleted.

## Method 2: Dynamic Programming β Top-Down Approach

Method 2 leverages dynamic programming with memoization. Each state in the recursion tree is saved to avoid redundant computations. This method is more efficient than brute force, especially for lists with repeating subproblems.

Here’s an example:

```def max_score(nums, memo={}):
if len(nums) == 0:
return 0
if tuple(nums) in memo:
return memo[tuple(nums)]
max_val = 0
for i in range(len(nums)):
curr_val = (nums[i-1] if i != 0 else 1) * (nums[i+1] if i < len(nums)-1 else 1)
max_val = max(max_val, curr_val + max_score(nums[:i] + nums[i+1:], memo))
memo[tuple(nums)] = max_val
return max_val

print(max_score([1, 2, 3]))```

Output:

`6`

In the updated `max_score` function, we add a memoization dictionary to store the results of calculated states. This prevents recalculation of the same states, which significantly improves performance. It’s a top-down approach because it starts with the full problem and breaks it down into subproblems.

## Method 3: Dynamic Programming β Bottom-Up Approach

Contrasting from the top-down, the bottom-up approach starts with the smallest subproblems and builds up the solution. It iteratively computes the optimal solution for larger and larger subproble