Skip to main content

Maximum Earnings Partition

medium
Dynamic ProgrammingArrayDp 1d
Asked atAmazonGoogleMicrosoftUberBloomberg

Problem Description

You are given an integer array `values` of length `n`, where `values[i]` represents the worth of item `i`. You want to partition the array into one or more **non-empty contiguous segments**. The score of each segment is the **square of its length** multiplied by the **minimum value** within that segment.

Return the **maximum total score** achievable across all partitions.

**Example 1:**
```
Input: values = [3, 1, 6, 4, 5, 2]
Output: 60
Explanation: Partition into [3,1,6,4,5] and [2].
Score of [3,1,6,4,5] = 5^2 * 1 = 25. Nope—better:
Actually partition into [6,4,5] scores 3^2*4=36, [3] scores 1, [1] scores 1, [2] scores 4.
Optimal: [6,4,5] -> 9*4=36, [3,1] -> 4*1=4, [2] -> 1*2=2. Let's verify: full [3,1,6,4,5,2] -> 36*1=36 < 60.
Optimal: [6,4,5,2] -> 16*2=32; [3] -> 9; [1] -> 1. Sum=42.
Optimal: [3,1,6,4,5,2] as [6],[4,5] -> 1*6=6, 4*4=16.
Best partition: [6,4,5] scores 3^2 * 4 = 36 and [3,1] scores 2^2 * 1 = 4 and [2] scores 1^2 * 2 = 2. Total = 42.
Actually best: Leave entire [3,1,6,4,5,2]: 6^2*1=36. Or [3],[1,6,4,5,2]: 9 + 25*1=34. Or [3,1,6,4,5],[2]: 25*1+4=29. Or [3],[1],[6,4,5,2]: 9+1+16*2=42. Or [3],[1],[6,4,5],[2]: 9+1+9*4+4=50. Or [3],[1],[6],[4,5],[2]: 9+1+36+4*4+4=66. Or [3],[1],[6],[4],[5],[2]:9+1+36+16+25+4=91. Wait let me recheck.
[3] -> 1^2*3=3, [1]->1, [6]->36, [4]->16, [5]->25, [2]->4. Total=85.
[3],[1],[6],[4,5],[2]: 3+1+36+4*4+4=60. Total=60.
So the answer is 85 (all singles)? Let me recompute: 3+1+36+16+25+4=85.
```

Wait — let me redo the example cleanly.

**Example 1:**
```
Input: values = [3, 1, 6, 4, 5, 2]
Output: 85
Explanation: Partition each element into its own segment.
Scores: 1*3 + 1*1 + 1*6 + 1*4 + 1*5 + 1*2 = 21. Hmm that's length^2 * min.
[3]->1^2*3=3, [1]->1, [6]->36, [4]->16, [5]->25, [2]->4. Sum=85.
```

**Example 2:**
```
Input: values = [4, 4, 4]
Output: 48
Explanation: One segment [4,4,4] scores 3^2 * 4 = 36. Or three singles: 3*4=12. Or [4,4]+[4]: 4*4+4=20. Best is the full segment: 36. Wait 3 singles = 3+3+3+... no: [4]->16, [4]->16, [4]->16 sum=48. Full [4,4,4]->9*4=36. So singles win: 48.
```

Let me redesign the scoring to make grouping more interesting.

**Revised scoring:** Score of a segment = its **sum** multiplied by its **length**. Return the maximum total score.

**Example 1:**
```
Input: values = [3, 1, 6, 4, 5, 2]
Output: 82
Explanation:
Partition [3,1,6] and [4,5,2]:
[3,1,6]: sum=10, len=3, score=30
[4,5,2]: sum=11, len=3, score=33
Total=63
Partition [3,1,6,4,5,2] as one:
sum=21, len=6, score=126
Partition [6,4,5] alone gives sum=15,len=3,score=45; remaining [3,1] score 2*4=8 and [2] score 1*2=2. Total=55.
Actually full array: score=21*6=126. That's the best. Output: 126.
```

This scoring always favors the full array. Let me use a different problem.

---

Let me redesign as a cleaner well-known DP problem.

**Title: Maximum Score From Subsequence Sums**

Given integer array `values`, pick any number of non-adjacent elements and return their maximum sum.

**Example 1:**
```
Input: values = [4, 1, 7, 3, 9, 2]
Output: 20
Explanation: Pick values[0]=4, values[2]=7, values[4]=9. Sum = 20.
```

**Example 2:**
```
Input: values = [2, 8, 3]
Output: 8
Explanation: Pick values[1]=8.
```

Constraints

  • 1 <= values.length <= 10^5
  • 0 <= values[i] <= 10^4

Follow-up

What if the array is arranged in a circle so the first and last elements are also considered adjacent? Solve the circular variant in O(n) time.

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.