Create Maximum Number LeetCode Solution

Last updated on October 5th, 2024 at 09:27 pm

Here, We see Create Maximum Number LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Dynamic Programming, Greedy

Companies

Google

Level of Question

Hard

Create Maximum Number LeetCode Solution

Create Maximum Number LeetCode Solution

Problem Statement

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]

1. Create Maximum Number LeetCode Solution C++

class Solution {
  public: vector &lt; int &gt; maxNumber(vector &lt; int &gt; &amp; nums1, vector &lt; int &gt; &amp; nums2, int k) {
    int n1 = nums1.size(), n2 = nums2.size();
    vector &lt; int &gt; best;
    for (int k1 = max(k - n2, 0); k1 &lt;= min(k, n1); ++k1)
      best = max(best, maxNumber(maxNumber(nums1, k1),
        maxNumber(nums2, k - k1)));
    return best;
  }
  vector &lt; int &gt; maxNumber(vector &lt; int &gt; nums, int k) {
    int drop = nums.size() - k;
    vector &lt; int &gt; out;
    for (int num: nums) {
      while (drop &amp;&amp; out.size() &amp;&amp; out.back() &lt; num) {
        out.pop_back();
        drop--;
      }
      out.push_back(num);
    }
    out.resize(k);
    return out;
  }
  vector &lt; int &gt; maxNumber(vector &lt; int &gt; nums1, vector &lt; int &gt; nums2) {
    vector &lt; int &gt; out;
    while (nums1.size() + nums2.size()) {
      vector &lt; int &gt; &amp; now = nums1 &gt; nums2 ? nums1 : nums2;
      out.push_back(now[0]);
      now.erase(now.begin());
    }
    return out;
  }
};

2. Create Maximum Number LeetCode Solution Java

class Solution {
  public int[] maxNumber(int[] nums1, int[] nums2, int k) {
    int n = nums1.length;
    int m = nums2.length;
    int[] ans = new int[k];
    for (int i = Math.max(0, k - m); i &lt;= k &amp;&amp; i &lt;= n; ++i) {
      int[] candidate = merge(maxArray(nums1, i), maxArray(nums2, k - i), k);
      if (greater(candidate, 0, ans, 0)) ans = candidate;
    }
    return ans;
  }
  private int[] merge(int[] nums1, int[] nums2, int k) {
    int[] ans = new int[k];
    for (int i = 0, j = 0, r = 0; r &lt; k; ++r)
      ans[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
    return ans;
  }
  public boolean greater(int[] nums1, int i, int[] nums2, int j) {
    while (i &lt; nums1.length &amp;&amp; j &lt; nums2.length &amp;&amp; nums1[i] == nums2[j]) {
      i++;
      j++;
    }
    return j == nums2.length || (i &lt; nums1.length &amp;&amp; nums1[i] &gt; nums2[j]);
  }
  public int[] maxArray(int[] nums, int k) {
    int n = nums.length;
    int[] ans = new int[k];
    for (int i = 0, j = 0; i &lt; n; ++i) {
      while (n - i + j &gt; k &amp;&amp; j &gt; 0 &amp;&amp; ans[j - 1] &lt; nums[i]) j--;
      if (j &lt; k) ans[j++] = nums[i];
    }
    return ans;
  }
}

3. Create Maximum Number Solution JavaScript

var maxNumber = function(nums1, nums2, k) {
	const maxArray = (nums, k) =&gt; {
		const stack = [];
		let popCount = nums.length - k;
		for (const num of nums) {
			while (popCount &gt; 0 &amp;&amp; stack.length &amp;&amp; stack[stack.length - 1] &lt; num) {
				stack.pop();
				popCount--;
			}
			stack.push(num);
		}
		return stack.slice(0, k);
	};
	const mergeArrays = (arr1, arr2) =&gt; {
		const merged = [];
		while (arr1.length || arr2.length) {
			const bigger = arr1 &gt; arr2 ? arr1 : arr2;
			merged.push(bigger.shift());
		}
		return merged;
	};
	let max = [];
	for (let i = Math.max(0, k - nums2.length); i &lt;= Math.min(k, nums1.length); i++) {
		const merged = mergeArrays(maxArray(nums1, i), maxArray(nums2, k - i));
		if (merged &gt; max) max = merged;
	}
	return max;
};

4. Create Maximum Number Solution Python

class Solution(object):
    def maxNumber(self, nums1, nums2, k):
        def prep(nums, k):
            drop = len(nums) - k
            out = []
            for num in nums:
                while drop and out and out[-1] &lt; num:
                    out.pop()
                    drop -= 1
                out.append(num)
            return out[:k]
        def merge(a, b):
            return [max(a, b).pop(0) for _ in a + b]
        return max(
            merge(prep(nums1, i), prep(nums2, k - i))
            for i in range(k + 1)
            if i &lt;= len(nums1) and k - i &lt;= len(nums2)
        )
Scroll to Top