LeetCode: Maximum Score From Performing Multiplication Operations Solution
1function recursion(l, i, nums, muls, memo) {2 if (i === muls.length) {3 return 04 }5 if (memo[l][i] != null) {6 return memo[l][i]7 }89 let left = l10 let right = nums.length - (i - l) - 111 let case1 = muls[i] * nums[left] + recursion(l + 1, i + 1, nums, muls, memo)12 let case2 = muls[i] * nums[right] + recursion(l, i + 1, nums, muls, memo)13 memo[l][i] = Math.max(case1, case2)1415 return memo[l][i]16}1718/**19 * @param {number[]} nums20 * @param {number[]} multipliers21 * @return {number}22 */23var maximumScore = function (nums, multipliers) {24 let memo = Array.from({ length: multipliers.length }, _ =>25 Array(multipliers.length).fill(null)26 )27 return recursion(0, 0, nums, multipliers, memo)28}
Comments
Loading comments...
Tags
leetcode
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
LeetCode: Broken Calculator
Feb 21, 2021