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.lengthn == nums2.length1 <= m, n <= 5000 <= nums1[i], nums2[i] <= 91 <= k <= m + nnums1 and nums2 do not have leading zeros.Problem Overview: You are given two arrays of digits and an integer k. Pick digits from both arrays (preserving their relative order) to build the largest possible number of length k. The challenge is choosing the best subsequence from each array and merging them so the final number is lexicographically maximum.
Approach 1: Greedy Combine with Monotonic Stack (O((m+n)*k) time, O(m+n) space)
The optimal strategy splits the problem into two steps: choose the best subsequence from each array, then merge them into the largest possible result. For every valid split i where i digits come from nums1 and k-i from nums2, compute the maximum subsequence using a monotonic decreasing stack. Iterate through each array and pop smaller digits while you still have removals available, ensuring the remaining sequence is lexicographically largest.
After extracting the best subsequences, merge them greedily. Compare the remaining portions of both sequences and always append the one that forms the larger lexicographical suffix. This comparison step ensures that equal prefixes still produce the globally maximum result. The approach relies heavily on concepts from Greedy strategies and the Monotonic Stack pattern.
This method works because each subsequence is locally optimal and the merge step guarantees global optimality. Iterating across all splits ensures you evaluate every valid combination of contributions from both arrays.
Approach 2: Dynamic Programming Approach (O(m * n * k) time, O(m * n * k) space)
A dynamic programming formulation treats the problem as constructing the best number while iterating through both arrays. Define a DP state representing the best sequence of length t formed using the first i elements of nums1 and first j elements of nums2. For each step, either pick the current digit from nums1, pick from nums2, or skip elements while maintaining order.
The transition compares candidate sequences lexicographically and keeps the larger one. While conceptually straightforward, storing and comparing sequences inside the DP table increases both time and memory usage. This approach mainly serves as a conceptual baseline showing the full search space before optimization.
The DP model highlights how the greedy stack solution compresses the state space dramatically by extracting optimal subsequences first and only merging promising candidates. Arrays and sequence comparisons dominate the operations, which is why the problem often appears in discussions around Array manipulation and advanced greedy design.
Recommended for interviews: The greedy monotonic stack + merge approach is what interviewers expect. It demonstrates mastery of subsequence extraction, lexicographic comparison, and greedy reasoning. A brute-force or DP explanation shows you understand the search space, but the optimized greedy solution proves you can reduce complexity and handle edge cases efficiently.
This approach involves iterating through possible splits between the two arrays where you extract 'x' numbers from the first array and 'k-x' from the second. For each split, generate the largest possible subsequences using a monotonic stack technique and merge them to form the largest possible number of length 'k'.
For merging two subsequences, always choose the larger next number between the two subsequences using a comparison.
In this Python solution, the function merge combines two subsequences by greedily picking the larger leading number from both. The function get_max_subarray generates the largest subsequence of a given length using a monotonic stack which helps to maintain the largest numbers by comparing and dropping smaller elements.
Python
JavaScript
Time Complexity: O((m+n)k), where m and n are the lengths of nums1 and nums2 respectively. Space Complexity: O(m+n) for storing temporary stack elements.
This approach uses dynamic programming to precompute the maximum numbers of all potential lengths for both arrays separately. Then, you combine these precomputed maximum subsequences using a similar greedy merge approach.
The C++ solution leverages lambda functions for a modular design. Maximum subsequences and their merging are handled within concise functions. The versatility of vectors in C++ aids significantly in storing intermediate computational results.
Time Complexity: O((m+n)k) due to similar operations to the greedy one but optimized through DP structure. Space Complexity: O(m+n).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Greedy Combine Method | Time Complexity: O((m+n)k), where m and n are the lengths of nums1 and nums2 respectively. Space Complexity: O(m+n) for storing temporary stack elements. |
| Dynamic Programming Approach | Time Complexity: O((m+n)k) due to similar operations to the greedy one but optimized through DP structure. Space Complexity: O(m+n). |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Combine with Monotonic Stack | O((m+n)*k) | O(m+n) | Best general solution. Efficient for large arrays and expected in interviews. |
| Dynamic Programming | O(m * n * k) | O(m * n * k) | Conceptual or educational approach to explore the full decision space. |
321. (Hard) Create Maximum Number - Daily Leetcode (Day 73) • yeetcode • 9,191 views views
Watch 9 more video solutions →Practice Create Maximum Number with our built-in code editor and test cases.
Practice on FleetCode