<?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>max number Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/max-number/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/max-number/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 15:16:57 +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>max number Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/max-number/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 321. Create Maximum Number</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-321-create-maximum-number/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-321-create-maximum-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 14 Nov 2017 16:21:18 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Greedy]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[max number]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=796</guid>

					<description><![CDATA[<p>Problem: Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k &#60;= m + n from digits of the two. The relative&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-321-create-maximum-number/">花花酱 LeetCode 321. Create Maximum Number</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/YYduNJfzWaA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given two arrays of length <code>m</code> and <code>n</code> with digits <code>0-9</code> representing two numbers. Create the maximum number of length <code>k &lt;= m + n</code> from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the <code>k</code> digits. You should try to optimize your time and space complexity.</p>
<p><b>Example 1:</b></p>
<p>nums1 = <code>[3, 4, 6, 5]</code><br />
nums2 = <code>[9, 1, 2, 5, 8, 3]</code><br />
k = <code>5</code><br />
return <code>[9, 8, 6, 5, 3]</code></p>
<p><b>Example 2:</b></p>
<p>nums1 = <code>[6, 7]</code><br />
nums2 = <code>[6, 0, 4]</code><br />
k = <code>5</code><br />
return <code>[6, 7, 6, 0, 4]</code></p>
<p><b>Example 3:</b></p>
<p>nums1 = <code>[3, 9]</code><br />
nums2 = <code>[8, 9]</code><br />
k = <code>3</code><br />
return <code>[9, 8, 9]</code></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>题目大意：给你两个数字数组和k，返回从两个数组中选取k个数字能够组成的最大值。</p>
<h1><strong>Idea: </strong>Greedy + DP</h1>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/321-ep107-2.png"><img class="alignnone wp-image-804 size-full" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/321-ep107-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/321-ep107-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/321-ep107-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/321-ep107-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/321-ep107-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<h1><strong>Solution:</strong></h1>
<p>Time complexity: O(k * (n1+n2)^2)</p>
<p>Space complexity: O(n1+n2)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 26 ms
class Solution {
public:
    vector&lt;int&gt; maxNumber(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2, int k) {
        vector&lt;int&gt; ans;
        const int n1 = nums1.size();
        const int n2 = nums2.size();
        for (int i = max(0, k - n2); i &lt;= min(k, n1); ++i)
            ans = max(ans, maxNumber(maxNumber(nums1, i), 
                                     maxNumber(nums2, k - i)));
        return ans;
    }
private:    
    vector&lt;int&gt; maxNumber(const vector&lt;int&gt;&amp; nums, int k) {
        vector&lt;int&gt; ans(k);                
        int j = 0;
        for (int i = 0; i &lt; nums.size(); ++i) {
            while (j &gt; 0 &amp;&amp; nums[i] &gt; ans[j - 1] 
                   &amp;&amp; nums.size() - i &gt; k - j) --j;
            if (j &lt; k) ans[j++] = nums[i];
        }
        return ans;
    }
    
    vector&lt;int&gt; maxNumber(const vector&lt;int&gt;&amp; nums1, const vector&lt;int&gt;&amp; nums2) {
        vector&lt;int&gt; ans(nums1.size() + nums2.size());
        auto s1 = nums1.cbegin();
        auto e1 = nums1.cend();
        auto s2 = nums2.cbegin();
        auto e2 = nums2.cend();        
        int index = 0;
        while (s1 != e1 || s2 != e2)
            ans[index++] = 
              lexicographical_compare(s1, e1, s2, e2) ? *s2++ : *s1++;
        return ans;
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 18 ms
class Solution {
    public int[] maxNumber(int[] nums1, int[] nums2, int k) {
        int[] best = new int[0];
        for (int i = Math.max(0, k - nums2.length); 
                 i &lt;= Math.min(k, nums1.length); ++i)            
            best = max(best, 0, 
                       maxNumber(maxNumber(nums1, i), 
                                 maxNumber(nums2, k - i)), 0);
        return best;
    }
    
    private int[] maxNumber(int[] nums, int k) {
        int[] ans = new int[k];
        int j = 0;
        for (int i = 0; i &lt; nums.length; ++i) {
            while (j &gt; 0 &amp;&amp; nums[i] &gt; ans[j - 1] 
                &amp;&amp; nums.length - i &gt; k - j) --j;
            if (j &lt; k)
                ans[j++] = nums[i];
        }        
        return ans;
    }
    
    private int[] maxNumber(int[] nums1, int[] nums2) {
        int[] ans = new int[nums1.length + nums2.length];
        int s1 = 0;
        int s2 = 0;
        int index = 0;
        while (s1 != nums1.length || s2 != nums2.length)
            ans[index++] = max(nums1, s1, nums2, s2) == nums1 ? 
                           nums1[s1++] : nums2[s2++];
        return ans;
    }
    
    private int[] max(int[] nums1, int s1, int[] nums2, int s2) {
        for (int i = s1; i &lt; nums1.length; ++i) {
            int j = s2 + i - s1;
            if (j &gt;= nums2.length) return nums1;
            if (nums1[i] &lt; nums2[j]) return nums2;
            if (nums1[i] &gt; nums2[j]) return nums1;
        }
        return nums2;
    }
}</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-321-create-maximum-number/">花花酱 LeetCode 321. Create Maximum Number</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-321-create-maximum-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
