• 24.5%

https://leetcode.com/problems/create-maximum-number/

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.

#### cpp

https://discuss.leetcode.com/topic/36805/c-16ms-fastest-beats-97

C++ 16ms, FASTEST, beats 97%.

The basic idea:

To create max number of length k from two arrays, you need to create max number of length i from array one and max number of length k-i from array two, then combine them together. After trying all possible i, you will get the max number created from two arrays.

Optimization:

1. Suppose nums1 = [3, 4, 6, 5], nums2 = [9, 1, 2, 5, 8, 3], the maximum number you can create from nums1 is [6, 5] with length 2. For nums2, it’s [9, 8, 3] with length 3. Merging the two sequence, we have [9, 8, 6, 5, 3], which is the max number we can create from two arrays without length constraint. If the required length k<=5, we can simply trim the result to required length from front. For instance, if k=3, then [9, 8, 6] is the result.

2. Suppose we need to create max number with length 2 from num = [4, 5, 3, 2, 1, 6, 0, 8]. The simple way is to use a stack, first we push 4 and have stack [4], then comes 5 > 4, we pop 4 and push 5, stack becomes [5], 3 < 5, we push 3, stack becomes [5, 3]. Now we have the required length 2, but we need to keep going through the array in case a larger number comes, 2 < 3, we discard it instead of pushing it because the stack already grows to required size 2. 1 < 3, we discard it. 6 > 3, we pop 3, since 6 > 5 and there are still elements left, we can continue to pop 5 and push 6, the stack becomes [6], since 0 < 6, we push 0, the stack becomes [6, 0], the stack grows to required length again. Since 8 > 0, we pop 0, although 8 > 6, we can’t continue to pop 6 since there is only one number, which is 8, left, if we pop 6 and push 8, we can’t get to length 2, so we push 8 directly, the stack becomes [6, 8].

3. In the basic idea, we mentioned trying all possible length i. If we create max number for different i from scratch each time, that would be a waste of time. Suppose num = [4, 9, 3, 2, 1, 8, 7, 6], we need to create max number with length from 1 to 8. For i == 8, result is the original array. For i == 7, we need to drop 1 number from array, since 9 > 4, we drop 4, the result is [9, 3, 2, 1, 8, 7, 6]. For i == 6, we need to drop 1 more number, 3 < 9, skip, 2 < 3, skip, 1 < 2, skip, 8 > 1, we drop 1, the result is [9, 3, 2, 8, 7, 6]. For i == 5, we need to drop 1 more, but this time, we needn’t check from beginning, during last scan, we already know [9, 3, 2] is monotonically non-increasing, so we check 8 directly, since 8 > 2, we drop 2, the result is [9, 3, 8, 7, 6]. For i == 4, we start with 8, 8 > 3, we drop 3, the result is [9, 8, 7, 6]. For i == 3, we start with 8, 8 < 9, skip, 7 < 8, skip, 6 < 7, skip, by now, we’ve got maximum number we can create from num without length constraint. So from now on, we can drop a number from the end each time. The result is [9, 8, 7], For i == 2, we drop last number 7 and have [9, 8]. For i == 1, we drop last number 8 and have [9].

https://discuss.leetcode.com/topic/32298/short-python-ruby-c

Translated it to C++ as well now. Not as short anymore, but still decent. And C++ allows different functions with the same name, so I chose to do that here to show how nicely the maxNumber(nums1, nums2, k) problem can be based on the problems maxNumber(nums, k) and maxNumber(nums1, nums2), which would make fine problems on their own.

An alternative for maxNumber(nums1, nums2):

#### python

https://discuss.leetcode.com/topic/32298/short-python-ruby-c

Short Python / Ruby / C++

Solved it on my own but now I see others already posted this idea. Oh well, at least it’s short, particularly my merge function.

The last two lines can be combined, but I find it rather ugly and not worth it:
for i in range(max(k-len(nums2), 0), min(k, len(nums1))+1))

https://discuss.leetcode.com/topic/32281/share-my-python-solution-with-explanation

Share my Python solution with explanation

To create the max number from num1 and nums2 with k elements, we assume the final result combined by i numbers (denotes as left) from num1 and j numbers (denotes as right) from nums2, where i+j==k.

Obviously, left and right must be the maximum possible number in num1 and num2 respectively. i.e. num1 = [6,5,7,1] and i == 2, then left must be [7,1].

The final result is the maximum possible merge of all left and right.

So there’re 3 steps:

1. iterate i from 0 to k.
2. find max number from num1, num2 by select i , k-i numbers, denotes as left, right
3. find max merge of left, right

function maxSingleNumber select i elements from num1 that is maximum. The idea find the max number one by one. i.e. assume nums [6,5,7,1,4,2], selects = 3.

1st digit: find max digit in [6,5,7,1], the last two digits [4, 2] can not be selected at this moment.

2nd digits: find max digit in [1,4], since we have already selects 7, we should consider elements after it, also, we should leave one element out.

3rd digits: only one left [2], we select it. and function output [7,4,2]

function mergeMax find the maximum combination of left, and right.

#### java

https://discuss.leetcode.com/topic/32272/share-my-greedy-solution

Share my greedy solution

Many of the posts have the same algorithm. In short we can first solve 2 simpler problem

1. Create the maximum number of one array
2. Create the maximum number of two array using all of their digits.

For an long and detailed explanation see my blog here.

The algorithm is O((m+n)^3) in the worst case. It runs in 22 ms.

http://52.20.106.37/create-maximum-number/

Create Maximum Number

Solution
To solve this problem, first let’s look at simpler version:

Easy Version No. 1

Given one array of length n, create the maximum number of length k.

The solution to this problem is Greedy with the help of stack. The recipe is as following

• Initialize a empty stack
• Loop through the array nums
• pop the top of stack if it is smaller than nums[i] until
1. stack is empty
2. the digits left is not enough to fill the stack to size k
• if stack size < k push nums[i]
• Return stack

Since the stack length is known to be k, it is very easy to use an array to simulate the stack.
The time complexity is O(n) since each element is at most been pushed and popped once.

Java

Easy Version No. 2

Given two array of length m and n, create maximum number of length k = m + n.

OK, this version is a lot closer to our original problem with the exception that we will use all the digits we have.

Still, for this version, Greedy is the first thing come to mind. We have k decisions to make, each time will just need to decide ans[i] is from which of the two. It seems obvious, we should always choose the larger one right? This is correct, but the problem is what should we do if they are equal?

This is not so obvious. The correct answer is we need to see what behind the two to decide. For example,

nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
ans = [6, 7, 6, 0, 4]

We decide to choose the 6 from nums1 at step 1, because 7 > 0. What if they are equal again? We continue to look the next digit until they are not equal. If all digits are equal then choose any one is ok. The procedure is like the merge in a merge sort. However due to the “look next until not equal”, the time complexity is O(nm).

As @lixx2100 mentioned that it is possible to have a linear time merge algorithm based on suffix array. See here and here. But there isn’t a short implementation for suffix array construction in linear time.

Java

Final Solution

Now let’s go back to the real problem. First, we divide the k digits required into two parts, i and k-i. We then find the maximum number of length i in one array and the maximum number of length k-i in the other array using the algorithm in section 1. Now we combine the two results in to one array using the algorithm in section 2. After that we compare the result with the result we have and keep the larger one as final answer.

Java

Share my 21ms java solution with comments

To find the maximum ,we can enumerate how digits we should get from nums1 , we suppose it is i.

So , the digits from nums2 is K - i.

And we can use a stack to get the get maximum number(x digits) from one array.

OK, Once we choose two maximum subarray , we should combine it to the answer.

It is just like merger sort, but we should pay attention to the case: the two digital are equal.

we should find the digits behind it to judge which digital we should choose now.

In other words,we should judge which subarry is bigger than the other.

That’s all.

If you have any question or suggest, I am happy you can comment on my blog : Create Maximum Number.

Thanks, merry christmas :)

update:use stack to find max sub array and it runs 21ms now.( thanks to @dietpepsi )

/* Created by hrwhisper on 2015/11/23. http://www.hrwhisper.me/leetcode-create-maximum-number/ /