HardNo. 1770

Maximum Score from Performing Multiplication Operations — LeetCode 1770 Python Solution

Topics
Array, Dynamic Programming
Reading time
3 min

The 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.

Example

Input
nums = [1,2,3], multipliers = [3,2,1]
Output
14
Explanation
An optimal solution is as follows:

Python solution

PythonSolution
class Solution:
    def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
        @cache
        def f(i, j, k):
            if k >= m or i >= n or j < 0:
                return 0
            a = f(i + 1, j, k + 1) + nums[i] * multipliers[k]
            b = f(i, j - 1, k + 1) + nums[j] * multipliers[k]
            return max(a, b)

        n = len(nums)
        m = len(multipliers)
        return f(0, n - 1, 0)

Complexity

Time
O(n·m) (typical)
Space · auxiliary
O(n·m)or optimized

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 1770. Maximum Score from Performing Multiplication Operations is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
1774Closest Dessert CostMedium
0045Jump Game IIMedium
0053Maximum SubarrayMedium
0055Jump GameMedium
0063Unique Paths IIMedium
0064Minimum Path SumMedium

Frequently asked questions

How hard is LeetCode 1770. Maximum Score from Performing Multiplication Operations?

LeetCode 1770. Maximum Score from Performing Multiplication Operations is rated Hard on LeetCode.

What topics does LeetCode 1770. Maximum Score from Performing Multiplication Operations cover?

LeetCode 1770. Maximum Score from Performing Multiplication Operations is tagged Array and Dynamic Programming on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.