joshita / dev

Published

- 1 min read

Maximum score from performing multiplication operation

img of Maximum score from performing multiplication operation

Maximum score from performing multiplication

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 ith 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. Note that multipliers[0] corresponds to the first operation, multipliers[1] to the second operation, and so on. Remove x from nums. Return the maximum score after performing m operations

TotalValue

       private int maxDpScoreRecursion(int left, int right, int index, int [] nums, int [] multiplier, int dp[][]) {

       if(index == multiplier.length){
        return 0;
       } 
       if(dp[index][left] !=0 ){
        return dp[index][left];
       } 
       return dp[index][left] = Math.max(
                      nums[left]* multiplier[index] + maxDpScoreRecursion(left+1, right, index+1, nums, multiplier, dp),
                      nums[right]*multiplier[index] + maxDpScoreRecursion(left, right-1, index+1, nums, multiplier, dp));
    }