<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>range dp Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/range-dp/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/range-dp/</link>
	<description></description>
	<lastBuildDate>Tue, 23 Feb 2021 05:40:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>range dp Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/range-dp/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1770. Maximum Score from Performing Multiplication Operations</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1770-maximum-score-from-performing-multiplication-operations/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1770-maximum-score-from-performing-multiplication-operations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Feb 2021 08:08:12 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[range dp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8149</guid>

					<description><![CDATA[<p>You are given two integer arrays nums and multipliersof size n and m respectively, where n >= m. The arrays are 1-indexed. You begin with a score of&#160;0. You want to perform&#160;exactly&#160;m&#160;operations. On the&#160;ith&#160;operation&#160;(1-indexed),&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1770-maximum-score-from-performing-multiplication-operations/">花花酱 LeetCode 1770. Maximum Score from Performing Multiplication Operations</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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">
<iframe title="花花酱 LeetCode 1770. Maximum Score from Performing Multiplication Operations - 刷题找工作 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" allowfullscreen></iframe>
</div></figure>



<p>You are given two integer arrays <code>nums</code> and <code>multipliers</code>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>. The arrays are <strong>1-indexed</strong>.</p>



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



<ul><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>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum</strong>&nbsp;score after performing&nbsp;</em><code>m</code>&nbsp;<em>operations.</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3], multipliers = [3,2,1]
<strong>Output:</strong> 14
<strong>Explanation:</strong>&nbsp;An optimal solution is as follows:
- Choose from the end, [1,2,<strong><u>3</u></strong>], adding 3 * 3 = 9 to the score.
- Choose from the end, [1,<strong><u>2</u></strong>], adding 2 * 2 = 4 to the score.
- Choose from the end, [<strong><u>1</u></strong>], adding 1 * 1 = 1 to the score.
The total score is 9 + 4 + 1 = 14.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
<strong>Output:</strong> 102
<strong>Explanation: </strong>An optimal solution is as follows:
- Choose from the start, [<strong>-5</strong>,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
- Choose from the start, [<strong><u>-3</u></strong>,-3,-2,7,1], adding -3 * -5 = 15 to the score.
- Choose from the start, [<strong><u>-3</u></strong>,-2,7,1], adding -3 * 3 = -9 to the score.
- Choose from the end, [-2,7,<strong><u>1</u></strong>], adding 1 * 4 = 4 to the score.
- Choose from the end, [-2,<strong><u>7</u></strong>], adding 7 * 6 = 42 to the score. 
The total score is 50 + 15 - 9 + 4 + 42 = 102.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: DP</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1770-ep385-1.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/02/1770-ep385-2.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>dp(i, j) := max score we can get with nums[i~j] left.</p>



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



<p>Time complexity: O(m*m)<br>Space complexity: O(m*m)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++/Top-Down</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maximumScore(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; multipliers) {
    const int m = multipliers.size();
    const int n = nums.size();
    vector&lt;vector&lt;int&gt;&gt; cache(m, vector&lt;int&gt;(m, INT_MIN));
    function&lt;int(int, int)&gt; dp = [&amp;](int i, int j) {
      const int k = n - (j - i + 1);
      if (k == m) return 0;
      int&amp; ans = cache[i][k];
      if (ans != INT_MIN) return ans;
      return ans = max(dp(i + 1, j) + nums[i] * multipliers[k],
                       dp(i, j - 1) + nums[j] * multipliers[k]);
    };
    return dp(0, n - 1);
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">C++/Bottom-UP</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maximumScore(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; multipliers) {
    const int m = multipliers.size();
    const int n = nums.size();
    // dp[i][j] := max score of using first i elements and last j elements
    vector&lt;vector&lt;int&gt;&gt; dp(m + 1, vector&lt;int&gt;(m + 1));
    for (int k = 1; k &lt;= m; ++k)
      for (int i = 0, j = k - i; i &lt;= k; ++i, --j)
        dp[i][j] = max((i ? dp[i - 1][j] + nums[i - 1] * multipliers[k - 1] : INT_MIN),
                       (j ? dp[i][j - 1] + nums[n - j] * multipliers[k - 1] : INT_MIN));
    int ans = INT_MIN;
    for (int i = 0; i &lt;= m; ++i)
      ans = max(ans, dp[i][m - i]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1770-maximum-score-from-performing-multiplication-operations/">花花酱 LeetCode 1770. Maximum Score from Performing Multiplication Operations</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1770-maximum-score-from-performing-multiplication-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
