Start learning free

LESSON 476 OF 678 · STACK

Create Maximum Number

Practice O(n)

You get two arrays of digits, nums1 and nums2. Each digit is from 0 to 9. You also get a number k.

Pick k digits in total from the two lists. Line them up to make the largest possible k-digit number. Return its digits as an array.

Digits from the same list must keep their order from that list. Apart from that, digits from the two lists can mix in any way.

 

Example 1:

Input: nums1 = [5, 2, 8], nums2 = [7, 1, 6, 3], k = 4
Output: [8, 7, 6, 3]
Explanation: Take the 8 from nums1.
Then take 7, 6 and 3 from nums2, in their order.

Example 2:

Input: nums1 = [4, 9], nums2 = [4, 1, 5], k = 5
Output: [4, 9, 4, 1, 5]
Explanation: All five digits are used.
Both lists start with 4.
Taking the 4 of nums1 first lets its 9 come second.
49415 is larger than 44915.

Example 3:

Input: nums1 = [1, 1], nums2 = [3, 0, 2], k = 2
Output: [3, 2]
Explanation: Both digits come from nums2.
Its 0 is skipped.

 

Constraints:

  • nums1 and nums2 each have 1 to 30 digits.
  • k is from 1 to the number of digits in both lists together.
STEP 1 OF 28
    Ctrl + Enter to check