<?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>largest Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/largest/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/largest/</link>
	<description></description>
	<lastBuildDate>Sun, 10 Apr 2022 06:04:15 +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>largest Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/largest/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2231. Largest Number After Digit Swaps by Parity</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Apr 2022 06:03:37 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[largest]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9633</guid>

					<description><![CDATA[<p>You are given a positive integer&#160;num. You may swap any two digits of&#160;num&#160;that have the same&#160;parity&#160;(i.e. both odd digits or both even digits). Return&#160;the&#160;largest&#160;possible value&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/">花花酱 LeetCode 2231. Largest Number After Digit Swaps by Parity</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 a positive integer&nbsp;<code>num</code>. You may swap any two digits of&nbsp;<code>num</code>&nbsp;that have the same&nbsp;<strong>parity</strong>&nbsp;(i.e. both odd digits or both even digits).</p>



<p>Return<em>&nbsp;the&nbsp;<strong>largest</strong>&nbsp;possible value of&nbsp;</em><code>num</code><em>&nbsp;after&nbsp;<strong>any</strong>&nbsp;number of swaps.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 1234
<strong>Output:</strong> 3412
<strong>Explanation:</strong> Swap the digit 3 with the digit 1, this results in the number 3214.
Swap the digit 2 with the digit 4, this results in the number 3412.
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 65875
<strong>Output:</strong> 87655
<strong>Explanation:</strong> Swap the digit 8 with the digit 6, this results in the number 85675.
Swap the first digit 5 with the digit 7, this results in the number 87655.
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
</pre>



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



<ul><li><code>1 &lt;= num &lt;= 10<sup>9</sup></code></li></ul>



<p>Solution:</p>



<p>Put all even digits into one array, all odd digits into another one, all digits into the third. Sort two arrays, and generate a new number from sorted arrays.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int largestInteger(int num) {
    vector&lt;int&gt; odd, even, org;
    for (int cur = num; cur; cur /= 10) {
      ((cur &amp; 1) ? odd : even).push_back(cur % 10);
      org.push_back(cur % 10);
    }
    sort(begin(odd), end(odd));
    sort(begin(even), end(even));    
    int ans = 0;
    for (int i = 0; i &lt; org.size(); ++i) {
      if (i) ans *= 10;
      auto&amp; cur = (org[org.size() - i - 1] &amp; 1) ? odd : even;
      ans += cur.back();
      cur.pop_back();
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/">花花酱 LeetCode 2231. Largest Number After Digit Swaps by Parity</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/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 410. Split Array Largest Sum</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 04 Jul 2018 04:49:04 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[groups]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[largest]]></category>
		<category><![CDATA[subarray]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2986</guid>

					<description><![CDATA[<p>Problem Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/">花花酱 LeetCode 410. Split Array Largest Sum</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><iframe width="500" height="375" src="https://www.youtube.com/embed/_k-Jb4b7b_0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given an array which consists of non-negative integers and an integer <i>m</i>, you can split the array into <i>m</i> non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these <i>m</i> subarrays.</p>
<p><b>Note:</b><br />
If <i>n</i> is the length of array, assume the following constraints are satisfied:</p>
<ul>
<li>1 ≤ <i>n</i> ≤ 1000</li>
<li>1 ≤ <i>m</i> ≤ min(50, <i>n</i>)</li>
</ul>
<p><b>Examples:</b></p>
<pre class="crayon:false">Input:
<b>nums</b> = [7,2,5,10,8]
<b>m</b> = 2

Output:
18

Explanation:
There are four ways to split <b>nums</b> into two subarrays.
The best way is to split it into <b>[7,2,5]</b> and <b>[10,8]</b>,
where the largest sum among the two subarrays is only 18.
</pre>
<h1> <img class="alignnone size-full wp-image-2997" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2996" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2999" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><strong>Solution: DP</strong></h1>
<p>Time complexity: O(n^2*m)</p>
<p>Space complexity: O(n*m)</p>
<p>C++ / Recursion + Memorization</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 111 ms
class Solution {
public:
  int splitArray(vector&lt;int&gt;&amp; nums, int m) {
    const int n = nums.size();
    sums_ = vector&lt;int&gt;(n);
    mem_ = vector&lt;vector&lt;int&gt;&gt;(n, vector&lt;int&gt;(m + 1, INT_MAX));
    sums_[0] = nums[0];
    for (int i = 1; i &lt; n; ++i)
      sums_[i] = sums_[i - 1] + nums[i];
    return splitArray(nums, n - 1, m);
  }
private:
  vector&lt;vector&lt;int&gt;&gt; mem_;
  vector&lt;int&gt; sums_;
  
  // min of largest sum of spliting nums[0] ~ nums[k] into m groups
  int splitArray(const vector&lt;int&gt;&amp; nums, int k, int m) {
    if (m == 1) return sums_[k];
    if (m &gt; k + 1) return INT_MAX;    
    if (mem_[k][m] != INT_MAX) return mem_[k][m];
    int ans = INT_MAX;
    for (int i = 0; i &lt; k; ++i)
      ans = min(ans, max(splitArray(nums, i, m - 1), sums_[k] - sums_[i]));    
    return mem_[k][m] = ans;
  }
};</pre><p>C++ / DP</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 90 ms
class Solution {
public:
  int splitArray(vector&lt;int&gt;&amp; nums, int m) {
    const int n = nums.size();
    vector&lt;int&gt; sums(n);
    // dp[i][j] := min of largest sum of splitting nums[0] ~ nums[j] into i groups.
    vector&lt;vector&lt;int&gt;&gt; dp(m + 1, vector&lt;int&gt;(n, INT_MAX));
    sums[0] = nums[0];
    for (int i = 1; i &lt; n; ++i)
      sums[i] = sums[i - 1] + nums[i];
    for (int i = 0; i &lt; n; ++i)
      dp[1][i] = sums[i];
    
    for (int i = 2; i &lt;= m; ++i)
      for (int j = i - 1; j &lt; n; ++j)
        for (int k = 0; k &lt; j; ++k)
          dp[i][j] = min(dp[i][j], max(dp[i - 1][k], sums[j] - sums[k]));
    return dp[m][n - 1];
  }  
};</pre><p></p>
<h1><strong>Solution: Binary Search</strong></h1>
<p>Time complexity: O(log(sum(nums))*n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
  int splitArray(vector&lt;int&gt;&amp; nums, int m) {
    long l = *max_element(begin(nums), end(nums));
    long r = accumulate(begin(nums), end(nums), 0L) + 1;
    while (l &lt; r) {
      long limit = (r - l) / 2 + l;
      if (min_groups(nums, limit) &gt; m) 
        l = limit + 1;
      else
        r = limit;
    }
    return l;
  }
private:
  int min_groups(const vector&lt;int&gt;&amp; nums, long limit) {
    long sum = 0;
    int groups = 1;
    for (int num : nums) {
      if (sum + num &gt; limit) {
        sum = num;
        ++groups;
      } else {
        sum += num;
      }
    }    
    return groups;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/">花花酱 LeetCode 410. Split Array Largest Sum</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-410-split-array-largest-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
