{x}
blog image

Create Maximum Number

You are given two integer arrays nums1 and nums2 of lengths m and n respectively. nums1 and nums2 represent the digits of two numbers. You are also given an integer k.

Create the maximum number of length k <= m + n from digits of the two numbers. The relative order of the digits from the same array must be preserved.

Return an array of the k digits representing the answer.

 

Example 1:

Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
Output: [9,8,6,5,3]

Example 2:

Input: nums1 = [6,7], nums2 = [6,0,4], k = 5
Output: [6,7,6,0,4]

Example 3:

Input: nums1 = [3,9], nums2 = [8,9], k = 3
Output: [9,8,9]

 

Constraints:

  • m == nums1.length
  • n == nums2.length
  • 1 <= m, n <= 500
  • 0 <= nums1[i], nums2[i] <= 9
  • 1 <= k <= m + n
  • nums1 and nums2 do not have leading zeros.

Solution Explanation for Create Maximum Number

This problem asks to create the lexicographically largest number of length k by picking digits from two input arrays, nums1 and nums2, while preserving the relative order of digits within each array.

The solution employs a divide-and-conquer strategy combined with a monotonic stack and a merge function. Let's break down the code step by step:

1. f(nums, k) Function:

This function takes an array nums and an integer k as input. It aims to find the lexicographically largest sub-array of length k from nums. It uses a monotonic decreasing stack:

  • Monotonic Stack: The stack stk maintains a decreasing order. If a new element x is greater than the top of the stack and we still have elements to remove (remain > 0), we pop elements from the stack until the decreasing order is restored or the stack is empty or we've removed enough elements. This ensures we only keep the largest possible digits while maintaining the relative order.
  • Iteration: The loop iterates through nums. For each element, it checks if it can be added to the stack based on the monotonic condition.
  • Return: Returns the stk, which contains the lexicographically largest sub-array of length k.

2. compare(nums1, nums2, i, j) Function:

This function lexicographically compares two arrays nums1 and nums2 starting from indices i and j, respectively. It's a recursive helper function used to determine which digit to choose during the merge operation.

3. merge(nums1, nums2) Function:

This function merges two sorted arrays nums1 and nums2 to create the lexicographically largest array. It uses the compare function to determine which element to pick from nums1 and nums2 at each step.

4. Main Function maxNumber(nums1, nums2, k):

This is the main function that orchestrates the solution:

  • Looping Through Combinations: The outer loop iterates through all possible combinations of taking x digits from nums1 and k - x digits from nums2. The range of x is determined by l = max(0, k - n) and r = min(k, m) to ensure we don't exceed the bounds of the arrays.
  • Generating Sub-arrays: For each combination, it uses the f function to get the maximum number sub-arrays from nums1 and nums2.
  • Merging Sub-arrays: It then merges these sub-arrays using the merge function.
  • Comparing Results: Finally, it compares the current merged result with the best result obtained so far (ans) and updates ans if a better result is found.

Time Complexity Analysis:

  • f(nums, k): O(n), where n is the length of the input array. Each element is pushed and possibly popped once.
  • compare(nums1, nums2, i, j): O(min(len(nums1), len(nums2))). In the worst case, it compares all elements.
  • merge(nums1, nums2): O(m + n), where m and n are the lengths of input arrays.
  • maxNumber(nums1, nums2, k): The outer loop iterates at most min(m,k) + 1 times. Each iteration calls f twice and merge once. Therefore, the overall time complexity is O(mk + nk)

Space Complexity Analysis:

The space complexity is dominated by the size of the arrays created during the process. The maximum space used is O(m + n) to store the result and intermediate arrays.

In summary, the solution uses a clever combination of monotonic stack and merging to efficiently find the lexicographically largest number from two arrays. The time complexity is dominated by the merging and subarray selection steps, resulting in a near-linear time solution for the specified constraints.