This problem asks us to build the largest possible number of exactly k digits, where each digit comes from either nums1 or nums2, and the relative order of digits within each source array is preserved. Stated another way: pick a subsequence from nums1 and a subsequence from nums2 whose lengths sum to k, then interleave them into the largest possible number.
What makes this hard is the interplay of three decisions: how many digits to take from each array, which digits to pick from each array, and how to merge the two picks into the largest result. Getting any one of these wrong produces a suboptimal answer. Each decision can be solved as its own subproblem: find the maximum subsequence of a given length from a single array, merge two subsequences into the largest result, and try every valid split of k between the two arrays.
1 <= m, n <= 500 → Both arrays are small. Trying every split of k between the two arrays, with linear scans plus an O(k^2) merge per split, fits comfortably in the time limit.0 <= nums1[i], nums2[i] <= 9 → Only ten distinct values, so equal digits collide often and the merge step needs a tiebreak that looks past the front digit.1 <= k <= m + n → k can exceed either array's length, so some splits are forced to take digits from both arrays.Enumerate every possible way to pick k digits from the two arrays while preserving relative order, build each candidate number, and track the maximum.
This means generating all subsequences of every valid length from nums1, all subsequences of the complementary length from nums2, and every order-preserving interleaving of each pair. The counts explode: an array of length m has C(m, i) subsequences of length i, and a pair of picks can interleave in C(k, i) ways. With m and n up to 500, the candidate count is exponential.
Loading animation...
Almost all of this work is wasted. For a fixed length, only the maximum subsequence of each array matters: any other subsequence of the same length can be swapped for the maximum one without making the merged result smaller. The next approach computes that single best subsequence per array in linear time and merges one pair of candidates per split.
Instead of exploring all combinations, we decompose the problem into three subproblems:
For a fixed split, the answer must be built from the maximum subsequence of each array. If a candidate used a non-maximal subsequence from nums1, replacing it with the maximal subsequence of the same length never makes the merged result smaller, so checking only the two maximal subsequences cannot miss the optimum for that split.
In the merge, when the front digits differ, the larger one must go first: candidates are compared digit by digit from the left, so a larger digit in the current position beats any arrangement of the digits that follow. When the front digits are equal, the digit written is the same either way; the choice only affects which digits remain available. Taking from the side with the larger suffix keeps the stronger remaining digits closer to the front, which is what the [6, 7] and [6, 0, 4] example shows.
Loading animation...