> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Create Maximum Number Python Solution

> Tested Python solution for LeetCode 321 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 321, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Stack](/catalog/topics/stack), [Greedy](/catalog/topics/greedy), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/create-maximum-number/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 321   # by problem number
lcpy gen -s create_maximum_number   # by problem name
```

## Problem

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.

### Examples

```
Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
Output: [9,8,6,5,3]
```

```
Input: nums1 = [6,7], nums2 = [6,0,4], k = 5
Output: [6,7,6,0,4]
```

```
Input: nums1 = [3,9], nums2 = [8,9], k = 3
Output: [9,8,9]
```

### Constraints

* `m == nums1.length`
* `n == nums2.length`
* `1 <= m, n <= 500`
* `0 <= nums1[i], nums2[i] <= 9`
* `1 <= k <= m + n`
* `nums1` and `nums2` do not have leading zeros.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/create_maximum_number/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/create_maximum_number/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(k * (m + n))
    # Space: O(k)
    def max_number(self, nums1: list[int], nums2: list[int], k: int) -> list[int]:
        def pick(nums: list[int], size: int) -> list[int]:
            drop = len(nums) - size
            stack: list[int] = []
            for digit in nums:
                while drop and stack and stack[-1] < digit:
                    stack.pop()
                    drop -= 1
                stack.append(digit)
            return stack[:size]

        def merge(a: list[int], b: list[int]) -> list[int]:
            merged: list[int] = []
            i = j = 0
            while i < len(a) and j < len(b):
                if a[i:] > b[j:]:
                    merged.append(a[i])
                    i += 1
                else:
                    merged.append(b[j])
                    j += 1
            merged.extend(a[i:])
            merged.extend(b[j:])
            return merged

        best: list[int] = []
        for take1 in range(max(0, k - len(nums2)), min(k, len(nums1)) + 1):
            candidate = merge(pick(nums1, take1), pick(nums2, k - take1))
            if candidate > best:
                best = candidate
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(k \* (m + n)) | O(k) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.