> ## 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.

# Advantage Shuffle Python Solution with Tests

> Tested Python solution for LeetCode 870 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 870, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/advantage-shuffle/description/).

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

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

## Problem

You are given two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`.

Return *any* permutation of `nums1` that maximizes its **advantage** with respect to `nums2`.

### Examples

```
Input: nums1 = [2,7,11,15], nums2 = [1,10,4,11]
Output: [2,11,7,15]
```

```
Input: nums1 = [12,24,8,32], nums2 = [13,25,32,11]
Output: [24,32,8,12]
```

### Constraints

* `1 <= nums1.length <= 10^5`
* `nums2.length == nums1.length`
* `0 <= nums1[i], nums2[i] <= 10^9`

**Note:** Multiple permutations can achieve the maximum advantage, so any valid one is accepted. The tests assert that the result is a permutation of `nums1` that reaches the maximum possible advantage count.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def advantage_count(self, nums1: list[int], nums2: list[int]) -> list[int]:
        sorted1 = sorted(nums1)
        order = sorted(range(len(nums2)), key=lambda i: nums2[i])
        result = [0] * len(nums1)
        low = 0
        high = len(nums1) - 1
        for idx in reversed(order):
            if sorted1[high] > nums2[idx]:
                result[idx] = sorted1[high]
                high -= 1
            else:
                result[idx] = sorted1[low]
                low += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags


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