Maximum Score from Performing Multiplication Operations - Problem

You are given two 0-indexed integer arrays nums and multipliers of size n and m respectively, where n >= m.

You begin with a score of 0. You want to perform exactly m operations. On the i-th operation (0-indexed) you will:

  • Choose one integer x from either the start or the end of the array nums.
  • Add multipliers[i] * x to your score.
  • Remove x from nums.

Return the maximum score after performing m operations.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3], multipliers = [3,2,1]
Output: 14
💡 Note: Optimal picks: Take 3 (right) → score = 3×3 = 9. Take 2 (right) → score = 9 + 2×2 = 13. Take 1 (left) → score = 13 + 1×1 = 14. Total: 14
Example 2 — All From Left
$ Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
Output: 102
💡 Note: Take from left: -5×(-10)=50, -3×(-5)=15, -3×3=-9, -2×4=-8, 7×6=42. Total: 50+15-9-8+42 = 90. But optimal strategy gives 102.
Example 3 — Mixed Strategy
$ Input: nums = [1], multipliers = [5]
Output: 5
💡 Note: Only one element and one multiplier: 1×5 = 5

Constraints

  • n == nums.length
  • m == multipliers.length
  • 1 ≤ m ≤ 103
  • m ≤ n ≤ 105
  • -1000 ≤ nums[i], multipliers[i] ≤ 1000

Visualization

Tap to expand
Maximum Score from Multiplication Operations INPUT nums array: 1 2 3 index: 0 index: 1 index: 2 multipliers array: 3 2 1 i=0 i=1 i=2 Constraints: n = 3 (nums size) m = 3 (multipliers size) Perform exactly m operations Pick from start OR end Goal: Maximize score ALGORITHM STEPS 1 Op 0: Pick 3 (end) 3 * 3 = 9, score = 9 nums: [1, 2] 2 Op 1: Pick 2 (end) 2 * 2 = 4, score = 13 nums: [1] 3 Op 2: Pick 1 1 * 1 = 1, score = 14 nums: [] 4 DP State dp[i][j] = max score i = picks from left j = operation index Transition: dp[i][j] = max( left + dp[i+1][j+1], right + dp[i][j+1]) FINAL RESULT Total Score Calculation: Op 0: 3 x 3 = 9 Op 1: 2 x 2 = 4 Op 2: 1 x 1 = 1 Total: 9 + 4 + 1 = 14 OUTPUT 14 [OK] Maximum Score Optimal choices: end, end, start Selection Path: 1 2 3 --> 14 Key Insight: Use Dynamic Programming with state dp[i][j] where i = elements picked from left, j = current operation. Elements picked from right = j - i. At each step, choose max of picking from start or end. Time: O(m^2) | Space: O(m^2) can be optimized to O(m) TutorialsPoint - Maximum Score from Performing Multiplication Operations | Optimal Solution (Dynamic Programming)
Asked in
Facebook 15 Google 12 Amazon 8
98.0K Views
Medium Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen