Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum additive score by deleting numbers in Python
Suppose we have a list of numbers called nums. We can perform an operation where we select a number (not the first or last), remove it, and increase our score by the sum of that number and its two adjacent numbers. We have to find the maximal score possible by performing this operation as many times as we want.
So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 39. We can select 5, then sum will be (4 + 5 + 6) = 15, array becomes [2, 3, 4, 6]. Then select 4, sum is (3 + 4 + 6) = 13, array becomes [2, 3, 6]. Select 3, sum will be (2 + 3 + 6) = 11. Total sum is 15 + 13 + 11 = 39.
Algorithm
To solve this problem, we use dynamic programming with interval approach ?
- Create a 2D DP array where
dp[i][j]represents maximum score from subarray between indices i and j - For each interval of length 3 to n, calculate the maximum score
- For each possible middle element k, try removing it and add the score
- The score for removing element k is:
nums[i-1] + nums[k-1] + nums[j-1] + dp[i][k] + dp[k][j]
Python Implementation
def max_additive_score(nums):
n = len(nums)
if n < 3:
return 0
# Create DP table
dp = [[0] * (n + 1) for _ in range(n + 1)]
# For each interval length from 3 to n
for length in range(3, n + 1):
# For each starting position
for i in range(1, n - length + 2):
j = i + length - 1
max_score = 0
# Try removing each middle element k
for k in range(i + 1, j):
# Score = left_dp + right_dp + current_removal_score
current = dp[i][k] + dp[k][j] + nums[i-1] + nums[k-1] + nums[j-1]
max_score = max(max_score, current)
dp[i][j] = max_score
return dp[1][n]
# Test the function
nums = [2, 3, 4, 5, 6]
result = max_additive_score(nums)
print(f"Maximum additive score: {result}")
Maximum additive score: 39
Step-by-Step Execution
def max_additive_score_detailed(nums):
n = len(nums)
print(f"Input array: {nums}")
if n < 3:
return 0
dp = [[0] * (n + 1) for _ in range(n + 1)]
for length in range(3, n + 1):
print(f"\nProcessing intervals of length {length}:")
for i in range(1, n - length + 2):
j = i + length - 1
max_score = 0
for k in range(i + 1, j):
current = dp[i][k] + dp[k][j] + nums[i-1] + nums[k-1] + nums[j-1]
print(f" Interval [{i},{j}], removing element at {k}: score = {current}")
max_score = max(max_score, current)
dp[i][j] = max_score
print(f" dp[{i}][{j}] = {max_score}")
return dp[1][n]
# Detailed execution
nums = [2, 3, 4, 5, 6]
result = max_additive_score_detailed(nums)
print(f"\nFinal maximum score: {result}")
Input array: [2, 3, 4, 5, 6] Processing intervals of length 3: Interval [1,3], removing element at 2: score = 9 dp[1][3] = 9 Interval [2,4], removing element at 3: score = 12 dp[2][4] = 12 Interval [3,5], removing element at 4: score = 15 dp[3][5] = 15 Processing intervals of length 4: Interval [1,4], removing element at 2: score = 21 Interval [1,4], removing element at 3: score = 21 dp[1][4] = 21 Interval [2,5], removing element at 3: score = 27 Interval [2,5], removing element at 4: score = 27 dp[2][5] = 27 Processing intervals of length 5: Interval [1,5], removing element at 2: score = 39 Interval [1,5], removing element at 3: score = 36 Interval [1,5], removing element at 4: score = 36 dp[1][5] = 39 Final maximum score: 39
Time and Space Complexity
- Time Complexity: O(n³) − three nested loops for length, start position, and middle element
- Space Complexity: O(n²) − for the 2D DP table
Conclusion
This problem uses interval dynamic programming where we build up solutions for larger intervals from smaller ones. The key insight is that when we remove an element, we get its value plus its neighbors, and the remaining problem splits into two independent subproblems.
