{"id":8149,"date":"2021-02-21T00:08:12","date_gmt":"2021-02-21T08:08:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8149"},"modified":"2021-02-22T21:40:55","modified_gmt":"2021-02-23T05:40:55","slug":"leetcode-1770-maximum-score-from-performing-multiplication-operations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1770-maximum-score-from-performing-multiplication-operations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1770. Maximum Score from Performing Multiplication Operations"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1770. Maximum Score from Performing Multiplication Operations - \u5237\u9898\u627e\u5de5\u4f5c EP385\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/8sMdqZ8z1l0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given two integer arrays\u00a0<code>nums<\/code>\u00a0and\u00a0<code>multipliers<\/code>of size\u00a0<code>n<\/code>\u00a0and\u00a0<code>m<\/code>\u00a0respectively, where\u00a0<code>n >= m<\/code>. The arrays are\u00a0<strong>1-indexed<\/strong>.<\/p>\n\n\n\n<p>You begin with a score of&nbsp;<code>0<\/code>. You want to perform&nbsp;<strong>exactly<\/strong>&nbsp;<code>m<\/code>&nbsp;operations. On the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;operation&nbsp;<strong>(1-indexed)<\/strong>, you will:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose one integer&nbsp;<code>x<\/code>&nbsp;from&nbsp;<strong>either the start or the end&nbsp;<\/strong>of the array&nbsp;<code>nums<\/code>.<\/li><li>Add&nbsp;<code>multipliers[i] * x<\/code>&nbsp;to your score.<\/li><li>Remove&nbsp;<code>x<\/code>&nbsp;from the array&nbsp;<code>nums<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;score after performing&nbsp;<\/em><code>m<\/code>&nbsp;<em>operations.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3], multipliers = [3,2,1]\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong>&nbsp;An optimal solution is as follows:\n- Choose from the end, [1,2,<strong><u>3<\/u><\/strong>], adding 3 * 3 = 9 to the score.\n- Choose from the end, [1,<strong><u>2<\/u><\/strong>], adding 2 * 2 = 4 to the score.\n- Choose from the end, [<strong><u>1<\/u><\/strong>], adding 1 * 1 = 1 to the score.\nThe total score is 9 + 4 + 1 = 14.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]\n<strong>Output:<\/strong> 102\n<strong>Explanation: <\/strong>An optimal solution is as follows:\n- Choose from the start, [<strong>-5<\/strong>,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.\n- Choose from the start, [<strong><u>-3<\/u><\/strong>,-3,-2,7,1], adding -3 * -5 = 15 to the score.\n- Choose from the start, [<strong><u>-3<\/u><\/strong>,-2,7,1], adding -3 * 3 = -9 to the score.\n- Choose from the end, [-2,7,<strong><u>1<\/u><\/strong>], adding 1 * 4 = 4 to the score.\n- Choose from the end, [-2,<strong><u>7<\/u><\/strong>], adding 7 * 6 = 42 to the score. \nThe total score is 50 + 15 - 9 + 4 + 42 = 102.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>m == multipliers.length<\/code><\/li><li><code>1 &lt;= m &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>m &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-1000 &lt;= nums[i], multipliers[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-1.png\" alt=\"\" class=\"wp-image-8157\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-2.png\" alt=\"\" class=\"wp-image-8158\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1770-ep385-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>dp(i, j) := max score we can get with nums[i~j] left.<\/p>\n\n\n\n<p>k = n &#8211; (j &#8211; i + 1)<br>dp(i, j) = max(dp(i + 1, j) + nums[i] * multipliers[k], dp(i, j-1) + nums[j] * multipliers[k])<\/p>\n\n\n\n<p>Time complexity: O(m*m)<br>Space complexity: O(m*m)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Top-Down<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumScore(vector<int>& nums, vector<int>& multipliers) {\n    const int m = multipliers.size();\n    const int n = nums.size();\n    vector<vector<int>> cache(m, vector<int>(m, INT_MIN));\n    function<int(int, int)> dp = [&](int i, int j) {\n      const int k = n - (j - i + 1);\n      if (k == m) return 0;\n      int& ans = cache[i][k];\n      if (ans != INT_MIN) return ans;\n      return ans = max(dp(i + 1, j) + nums[i] * multipliers[k],\n                       dp(i, j - 1) + nums[j] * multipliers[k]);\n    };\n    return dp(0, n - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Bottom-UP<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumScore(vector<int>& nums, vector<int>& multipliers) {\n    const int m = multipliers.size();\n    const int n = nums.size();\n    \/\/ dp[i][j] := max score of using first i elements and last j elements\n    vector<vector<int>> dp(m + 1, vector<int>(m + 1));\n    for (int k = 1; k <= m; ++k)\n      for (int i = 0, j = k - i; i <= k; ++i, --j)\n        dp[i][j] = max((i ? dp[i - 1][j] + nums[i - 1] * multipliers[k - 1] : INT_MIN),\n                       (j ? dp[i][j - 1] + nums[n - j] * multipliers[k - 1] : INT_MIN));\n    int ans = INT_MIN;\n    for (int i = 0; i <= m; ++i)\n      ans = max(ans, dp[i][m - i]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integer arrays\u00a0nums\u00a0and\u00a0multipliersof size\u00a0n\u00a0and\u00a0m\u00a0respectively, where\u00a0n >= m. The arrays are\u00a01-indexed. You begin with a score of&nbsp;0. You want to perform&nbsp;exactly&nbsp;m&nbsp;operations. On the&nbsp;ith&nbsp;operation&nbsp;(1-indexed),&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177,697],"class_list":["post-8149","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-range-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8149"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8149\/revisions"}],"predecessor-version":[{"id":8159,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8149\/revisions\/8159"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}