Maximum Score from Performing Multiplication Operations - Problem
You're given two arrays: nums (size n) and multipliers (size m), where n >= m. Your goal is to maximize your score by performing exactly m operations.
In each operation i (starting from 0):
- Choose an element from either the start or end of the
numsarray - Multiply it by
multipliers[i]and add to your score - Remove the chosen element from
nums
The challenge is to decide whether to pick from the left or right end at each step to achieve the maximum possible score.
Example: If nums = [1,2,3] and multipliers = [3,2,1], you could pick 3 (right), then 2 (right), then 1 (left) for a score of 3×3 + 2×2 + 1×1 = 14.
Input & Output
example_1.py — Basic Case
$
Input:
nums = [1,2,3], multipliers = [3,2,1]
›
Output:
14
💡 Note:
Choose 3 (right) → 3×3=9. Choose 2 (right) → 2×2=4. Choose 1 (left) → 1×1=1. Total: 9+4+1=14.
example_2.py — Larger Array
$
Input:
nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
›
Output:
102
💡 Note:
Optimal sequence: take -5 (left), -3 (left), -3 (left), 1 (right), 7 (right). Score: (-5)×(-10) + (-3)×(-5) + (-3)×3 + 1×4 + 7×6 = 50+15-9+4+42 = 102.
example_3.py — Single Element
$
Input:
nums = [1], multipliers = [1]
›
Output:
1
💡 Note:
Only one choice: take the single element 1 and multiply by 1, giving score 1.
Visualization
Tap to expand
Understanding the Visualization
1
Setup
Cards laid out in a line, multipliers determine round values
2
Decision Point
Each turn, choose from leftmost or rightmost card
3
DP State
Track: operations completed + cards taken from left
4
Optimal Choice
At each state, pick the choice that maximizes future score
Key Takeaway
🎯 Key Insight: We only need to track two dimensions in our DP state: the current operation number and how many cards we've taken from the left. The right position can always be calculated from these two values!
Time & Space Complexity
Time Complexity
O(m²)
We fill an m×(m+1) DP table with constant work per cell
✓ Linear Growth
Space Complexity
O(m²)
DP table size, can be optimized to O(m) using rolling array
✓ Linear Space
Constraints
- n == nums.length
- m == multipliers.length
- 1 ≤ m ≤ min(103, n)
- 1 ≤ n ≤ 105
- -1000 ≤ nums[i], multipliers[i] ≤ 1000
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code