Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Create Maximum Number

Merge two arrays

Given two arrays of digits and an integer kk, create the maximum number of length kk from digits of both arrays. The relative order within each array must be preserved. For nums1=[3,4,6,5]nums1 = [3,4,6,5], nums2=[9,1,2,5,8,3]nums2 = [9,1,2,5,8,3], k=5k = 5, the answer is [9,8,6,5,3][9,8,6,5,3]. It combines "max number from one array" with "merge two arrays maximally." Break it into subproblems. Constraint: you must preserve relative order within each array.

If nums1=[6,7]nums1 = [6,7] and you pick 66, you cannot later pick 77 without 66. The split must satisfy i+j=ki + j = k and ilen(nums1)i \leq len(nums1), jlen(nums2)j \leq len(nums2).