Python solution with detailed explanation
Solution\n\nCreate Maximum Number https://leetcode.com/problems/create-maximum-number/\n\nDynamic Programming/Recursive Solution\n Sketch the recursion and thro
**Solution**\n\n**Create Maximum Number** https://leetcode.com/problems/create-maximum-number/\n\n**Dynamic Programming/Recursive Solution**\n* Sketch the recursion and throw in a cache. The complexity will be O(i * j * k). \n* The recursion is interesting in terms of the number of cases. When you have i,j, you need to think in how many different ways you can advance i and j with and without choosing nums1[i] and nums2[j]. Refer to the code, especially:\n```\n            self.max_number(i, j+1, k, nums1, nums2, so_far)            \n            self.max_number(i+1, j, k, nums1, nums2, so_far)            \n            self.max_number(i+1, j+1, k, nums1, nums2, so_far)\n```\n\n```\nclass Solution(object):\n    def max_number(self, i, j, k, nums1, nums2, so_far):\n        if len(so_far) == k:\n            int_number = int("".join(map(str, so_far)))\n            if int_number > self.maximum_number:\n                self.maximum_number = int_number\n                self.max_list = [x for x in so_far]\n        elif 0<=i<len(nums1) and 0<=j<len(nums2):\n            if nums1[i] > nums2[j]:\n                so_far.append(nums1[i])\n                self.max_number(i+1, j, k, nums1, nums2, so_far)\n                so_far.pop()\n            elif nums1[i] < nums2[j]:\n                so_far.append(nums2[j])\n                self.max_number(i, j+1, k, nums1, nums2, so_far)\n                so_far.pop()\n            elif nums1[i] == nums2[j]:\n                so_far.append(nums1[i])\n                self.max_number(i+1, j, k, nums1, nums2, so_far)\n                so_far.pop()                \n                so_far.append(nums2[j])\n                self.max_number(i, j+1, k, nums1, nums2, so_far)\n                so_far.pop()\n            # Ignore nums[i]    \n            self.max_number(i, j+1, k, nums1, nums2, so_far)\n            # ignore nums[i]\n            self.max_number(i+1, j, k, nums1, nums2, so_far)\n            # Ignore both\n            self.max_number(i+1, j+1, k, nums1, nums2, so_far)\n        elif i == len(nums1) and 0<=j<len(nums2):\n            so_far.append(nums2[j])\n            self.max_number(i, j+1, k, nums1, nums2, so_far)\n            so_far.pop()\n            self.max_number(i, j+1, k, nums1, nums2, so_far)\n        elif j == len(nums2) and 0<=i<len(nums1):\n            so_far.append(nums1[i])\n            self.max_number(i+1, j, k, nums1, nums2, so_far)\n            so_far.pop()\n            self.max_number(i+1, j, k, nums1, nums2, so_far)\n        return\n    \n    def maxNumber(self, nums1, nums2, k):\n        """\n        :type nums1: List[int]\n        :type nums2: List[int]\n        :type k: int\n        :rtype: List[int]\n        """\n        N, M = len(nums1), len(nums2)\n        self.maximum_number, self.max_list = -2**31, None\n        self.max_number(0, 0, k, nums1, nums2, [])\n        return self.max_list\n```\n\n**Optimized Solution using Stacks**\n* There is a smarter solution to this problem which is a combination of 2 smaller problems. \n* The solution to this problem will comprise of k digits, i from nums1 and k-i from nums2. \n* Assume i were 2, then what would be the best candidate? Clearly, the answer is "maximum number of size 2 from nums1 while keeping the relative order of the digits". \n* So lets call that problem as P0: "Given an array nums and an integer n, return the maximum number of size n that can be formed from nums while keeping the relative order of digits". Now for i=2, we will have 2 lists from nums1 and nums2 of size i and K-1. \n* We can merge them to compute a candidate solution of size k. This will be problem P1\n* What can be all the possible candidate solutions? Clearly when i will range from 0 to K for nums1 we will have a corresponding pair from nums2 ranging in size K to 0. Merging each of these pairs will give us the K+1 candidates. We then choose the largest from this pool and we are done!\nhttps://discuss.leetcode.com/topic/32281/share-my-python-solution-with-explanation\n\nP1: Merge 2 arrays with size i and K-1 formed from nums1 and nums2 to return the maximum possible number array of size K.\n* This is just like merge-sort merging, except when the elements are same. In that case we need to look beyond the current element (skipping all equal elements) to find the first unequal element and then pick the one which has the higher unequal element. Notice that when nums1[i] (or nums2[j]) is the last element and the value is equal to nums2[j] (or nums1[i]), we always pick the from the sublist which has elements remaining since there is a possibility of finding a greater solution.\n```\n                x,y = i, j\n                while x < N and y < M and left[x] == right[y]:\n                    x,y = x+1, y+1\n                if x != N and y != M and left[x] > right[y]:\n                    result.append(left[i])\n                    i += 1\n                elif x != N and y != M and left[x] < right[y]:\n                    result.append(right[j])\n                    j += 1\n                elif x == N:\n                    result.append(right[j])\n                    j += 1\n                elif y == M:\n                    result.append(left[i])\n                    i += 1\n```\n* Notice that in Python we can compare 2 lists l1 > l2 (merge_solution function) which does the above.\n```\n if nums1 > nums2:\n                ans += nums1[0],\n```\n\nP0: Given an array nums and an integer n, return the maximum number of size n that can be formed from nums while keeping the relative order of digits.\n\n**Method 1**\n* We can maintain a stack. As long as we get input in descending order, just keep pushing on the stack. if you get a number larger than top of stack, then pop till you find a smaller number.While doing this, make sure you do not pop so much that you cannot meet the constraint of K digits. Refer to the code in max_number_list.\n\n**Method 2**\n* Say the size of array is N = 10. Say K = 6. What is the maximum number that can be formed of size N = 10? Answer is the original array. What about K = 6?\n* This problem can be stated as: "Remove k1 (4) digits from the array to form the largest number". This is the opposite of https://leetcode.com/problems/remove-k-digits/ where we remove K digits to form the smallest number\n* The solution to the smallest number problem was to scan from left to right and remove the first peak element and then the next peak from the solution of previous.\n* The solution to this problem is opposite - remove the first trough element, i.e. element which is smaller than both its left and right side!\n\n```\nclass Solution(object):\n    def max_number_list(self, nums, n):\n        st, required_to_remove = [], len(nums)-n\n        for idx, x in enumerate(nums):\n            while required_to_remove and st and st[-1] < x:\n                st.pop()\n                required_to_remove = required_to_remove - 1\n            st.append(x)\n        while len(st) > n:\n            st.pop()\n        return st\n    \n    def merge(self, left, right):\n        i, j = 0, 0\n        N, M = len(left), len(right)\n        result = []\n        while i < N or j < M:\n            if i == N:\n                result.append(right[j])\n                j += 1\n            elif j == M:\n                result.append(left[i])\n                i += 1\n            elif left[i] < right[j]:\n                result.append(right[j])\n                j += 1\n            elif left[i] > right[j]:\n                result.append(left[i])\n                i += 1\n            else:\n                x,y = i, j\n                while x < N and y < M and left[x] == right[y]:\n                    x,y = x+1, y+1\n                if x != N and y != M and left[x] > right[y]:\n                    result.append(left[i])\n                    i += 1\n                elif x != N and y != M and left[x] < right[y]:\n                    result.append(right[j])\n                    j += 1\n                elif x == N:\n                    result.append(right[j])\n                    j += 1\n                elif y == M:\n                    result.append(left[i])\n                    i += 1\n        return result\n\n    def merge_solution(self, nums1, nums2):\n        ans = []\n        while nums1 or nums2:\n            if nums1 > nums2:\n                ans += nums1[0],\n                nums1 = nums1[1:]\n            else:\n                ans += nums2[0],\n                nums2 = nums2[1:]\n        return ans\n\n    def maxNumber(self, nums1, nums2, k):\n        """\n        :type nums1: List[int]\n        :type nums2: List[int]\n        :type k: int\n        :rtype: List[int]\n        """\n        N, M = len(nums1), len(nums2)\n        self.max_list = [0]*k\n        for i in range(k+1):\n            if i <= N and k-i <= M:\n                left, right = self.max_number_list(nums1, i), self.max_number_list(nums2, k-i)\n                merged = self.merge(left, right)\n                for i in range(k):\n                    if self.max_list[i] != merged[i]:\n                        if self.max_list[i] > merged[i]:\n                            break\n                        elif merged[i] > self.max_list[i]:\n                            self.max_list = merged\n                            break\n        return self.max_list\n```
[]