<?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>product Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/product/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/product/</link>
	<description></description>
	<lastBuildDate>Sun, 10 Apr 2022 06:38:40 +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>product Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/product/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2233. Maximum Product After K Increments</title>
		<link>https://zxi.mytechroad.com/blog/priority-queue/leetcode-2233-maximum-product-after-k-increments/</link>
					<comments>https://zxi.mytechroad.com/blog/priority-queue/leetcode-2233-maximum-product-after-k-increments/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Apr 2022 06:38:03 +0000</pubDate>
				<category><![CDATA[Priority Queue]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[product]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9640</guid>

					<description><![CDATA[<p>You are given an array of non-negative integers&#160;nums&#160;and an integer&#160;k. In one operation, you may choose&#160;any&#160;element from&#160;nums&#160;and&#160;increment&#160;it by&#160;1. Return&#160;the&#160;maximum&#160;product&#160;of&#160;nums&#160;after&#160;at most&#160;k&#160;operations.&#160;Since the answer may be very&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-2233-maximum-product-after-k-increments/">花花酱 LeetCode 2233. Maximum Product After K Increments</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an array of non-negative integers&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>. In one operation, you may choose&nbsp;<strong>any</strong>&nbsp;element from&nbsp;<code>nums</code>&nbsp;and&nbsp;<strong>increment</strong>&nbsp;it by&nbsp;<code>1</code>.</p>



<p>Return<em>&nbsp;the&nbsp;<strong>maximum</strong>&nbsp;<strong>product</strong>&nbsp;of&nbsp;</em><code>nums</code><em>&nbsp;after&nbsp;<strong>at most</strong>&nbsp;</em><code>k</code><em>&nbsp;operations.&nbsp;</em>Since the answer may be very large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,4], k = 5
<strong>Output:</strong> 20
<strong>Explanation:</strong> Increment the first number 5 times.
Now nums = [5, 4], with a product of 5 * 4 = 20.
It can be shown that 20 is maximum product possible, so we return 20.
Note that there may be other ways to increment nums to have the maximum product.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [6,3,3,2], k = 2
<strong>Output:</strong> 216
<strong>Explanation:</strong> Increment the second number 1 time and increment the fourth number 1 time.
Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
It can be shown that 216 is maximum product possible, so we return 216.
Note that there may be other ways to increment nums to have the maximum product.
</pre>



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



<ul><li><code>1 &lt;= nums.length, k &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: priority queue</strong></h2>



<p>Always increment the smallest number. Proof?</p>



<p>Time complexity: O(klogn + nlogn)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maximumProduct(vector&lt;int&gt;&amp; nums, int k) {
    constexpr int kMod = 1e9 + 7;
    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; q(begin(nums), end(nums));
    while (k--) {
      const int n = q.top(); 
      q.pop();
      q.push(n + 1);
    }
    long long ans = 1;
    while (!q.empty()) {
      ans *= q.top(); q.pop();
      ans %= kMod;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-2233-maximum-product-after-k-increments/">花花酱 LeetCode 2233. Maximum Product After K Increments</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/priority-queue/leetcode-2233-maximum-product-after-k-increments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 238. Product of Array Except Self</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-238-product-of-array-except-self/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-238-product-of-array-except-self/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 16 Jul 2018 14:51:27 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[product]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3191</guid>

					<description><![CDATA[<p>Problem: Given an array nums of n integers where n &#62; 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Note: Please&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-238-product-of-array-except-self/">花花酱 LeetCode 238. Product of Array Except Self</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem:</strong></h1>
<p>Given an array <code>nums</code> of <em>n</em> integers where <em>n</em> &gt; 1,  return an array <code>output</code> such that <code>output[i]</code> is equal to the product of all the elements of <code>nums</code> except <code>nums[i]</code>.</p>
<p><b>Example:</b></p>
<pre class="crayon:false"><b>Input:</b>  <code>[1,2,3,4]</code> <b>Output:</b> <code>[24,12,8,6]</code></pre>
<p><strong>Note: </strong>Please solve it <strong>without division</strong> and in O(<em>n</em>).</p>
<p><strong>Follow up:</strong><br />
Could you solve it with constant space complexity? (The output array <strong>does not</strong> count as extra space for the purpose of space complexity analysis.)</p>
<h1><strong>Solution: DP</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 36 ms
class Solution {
public:
  vector&lt;int&gt; productExceptSelf(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; l(n, 1); // l[i] = prod(nums[0] ~ nums[i - 1])
    vector&lt;int&gt; r(n, 1); // l[i] = prod(nums[i + 1] ~ nums[n - 1])
    vector&lt;int&gt; ans(n);    
    
    for (int i = 1; i &lt; n; ++i)
      l[i] = l[i - 1] * nums[i - 1];
    
    for (int i = n - 2; i &gt;= 0; --i)
      r[i] = r[i + 1] * nums[i + 1];
        
    for (int i = 0; i &lt; n; ++i)
      ans[i] = l[i] * r[i];
    
    return ans;
  }
};</pre><p></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms
class Solution {
public:
  vector&lt;int&gt; productExceptSelf(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int l = 1; // l = prod(nums[0] ~ nums[i - 1])
    int r = 1; // r = prod(nums[i + 1] ~ nums[n - 1])
    vector&lt;int&gt; ans(n, 1);
        
    for (int i = 0; i &lt; n; ++i) {
      ans[i] *= l;
      ans[n - i - 1] *= r;
      l *= nums[i];
      r *= nums[n - i - 1];
    }
    
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-238-product-of-array-except-self/">花花酱 LeetCode 238. Product of Array Except Self</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-238-product-of-array-except-self/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
