Skip to content

Commit b6ae9de

Browse files
committed
New Problem Solution -"1770. Maximum Score from Performing Multiplication Operations"
1 parent 6ef88aa commit b6ae9de

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ LeetCode
3737
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/) | [C++](./algorithms/cpp/closestDessertCost/ClosestDessertCost.cpp)|Medium|
3838
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [C++](./algorithms/cpp/countItemsMatchingARule/CountItemsMatchingARule.cpp)|Easy|
3939
|1771|[Maximize Palindrome Length From Subsequences](https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/) | [C++](./algorithms/cpp/maximizePalindromeLengthFromSubsequences/MaximizePalindromeLengthFromSubsequences.cpp)|Hard|
40+
|1770|[Maximum Score from Performing Multiplication Operations](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/) | [C++](./algorithms/cpp/maximumScoreFromPerformingMultiplicationOperations/MaximumScoreFromPerformingMultiplicationOperations.cpp)|Medium|
4041
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C++](./algorithms/cpp/minimumNumberOfOperationsToMoveAllBallsToEachBox/MinimumNumberOfOperationsToMoveAllBallsToEachBox.cpp)|Medium|
4142
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [C++](./algorithms/cpp/mergeStringsAlternately/MergeStringsAlternately.cpp)|Easy|
4243
|1765|[Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak/) | [C++](./algorithms/cpp/mapOfHighestPeak/MapOfHighestPeak.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Source : https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/
2+
// Author : Hao Chen
3+
// Date : 2021-04-01
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given two integer arrays nums and multipliers of size n and m respectively, where n >= m.
8+
* The arrays are 1-indexed.
9+
*
10+
* You begin with a score of 0. You want to perform exactly m operations. On the i^th operation
11+
* (1-indexed), you will:
12+
*
13+
* Choose one integer x from either the start or the end of the array nums.
14+
* Add multipliers[i] * x to your score.
15+
* Remove x from the array nums.
16+
*
17+
* Return the maximum score after performing m operations.
18+
*
19+
* Example 1:
20+
*
21+
* Input: nums = [1,2,3], multipliers = [3,2,1]
22+
* Output: 14
23+
* Explanation: An optimal solution is as follows:
24+
* - Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.
25+
* - Choose from the end, [1,2], adding 2 * 2 = 4 to the score.
26+
* - Choose from the end, [1], adding 1 * 1 = 1 to the score.
27+
* The total score is 9 + 4 + 1 = 14.
28+
*
29+
* Example 2:
30+
*
31+
* Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
32+
* Output: 102
33+
* Explanation: An optimal solution is as follows:
34+
* - Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
35+
* - Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.
36+
* - Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.
37+
* - Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.
38+
* - Choose from the end, [-2,7], adding 7 * 6 = 42 to the score.
39+
* The total score is 50 + 15 - 9 + 4 + 42 = 102.
40+
*
41+
* Constraints:
42+
*
43+
* n == nums.length
44+
* m == multipliers.length
45+
* 1 <= m <= 10^3
46+
* m <= n <= 10^5
47+
* -1000 <= nums[i], multipliers[i] <= 1000
48+
******************************************************************************************************/
49+
50+
const int MAX_SIZE = 1000;
51+
class Solution {
52+
private:
53+
int cache[MAX_SIZE][MAX_SIZE]; // num of left picked, num of right picked.
54+
int m, n;
55+
public:
56+
int maximumScore(vector<int>& nums, vector<int>& multipliers) {
57+
memset(cache, -1, sizeof(cache));
58+
n = nums.size();
59+
m = multipliers.size();
60+
return maximumScoreDFS(nums, 0, n-1, multipliers, 0 );
61+
}
62+
63+
int maximumScoreDFS(vector<int>& nums, int left, int right,
64+
vector<int>& multipliers, int midx) {
65+
66+
if(midx >= m ) return 0;
67+
68+
int nLeft = left; // num of left nums[] picked.
69+
int nRight = (n-1)-right; // num of right nums[] picked.
70+
if (cache[nLeft][nRight]!=-1) return cache[nLeft][nRight];
71+
72+
int pickLeft = maximumScoreDFS(nums, left+1, right, multipliers, midx+1) +
73+
multipliers[midx] * nums[left];
74+
75+
int pickRight = maximumScoreDFS(nums, left, right-1, multipliers, midx+1) +
76+
multipliers[midx] * nums[right];
77+
78+
cache[nLeft][nRight] = max(pickLeft, pickRight);
79+
return cache[nLeft][nRight];
80+
}
81+
};

0 commit comments

Comments
 (0)