2Brute2Best
🔥0

Advantage Shuffle

Medium·LeetCode #870 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You have two lists of the same length, nums1 and nums2. Think of nums1 as your team and nums2 as the opponents. Position i is a fight between your value and the opponent at position i.

You may reorder your team in any way, but you must keep every value. After the reorder, a fight is a win when your value is strictly bigger than the opponent's. The advantage is the number of wins.

Return a reordering of nums1 with the largest advantage. If several reorderings tie, any of them is fine.

Input

Two arrays nums1 and nums2 of the same length. Every value is 0 or more.

Output

A list: a reordering of nums1 that gives the largest advantage against nums2.

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

All four fights are wins: 2 > 1, 11 > 10, 7 > 4 and 15 > 11. That is the most possible.

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

Three fights are wins: 24 > 13, 32 > 25 and 12 > 11. The 32 opponent can't be beaten by anything we have, so the 8 goes there and the fight is lost.

Constraints
  • • 1 ≤ nums1.length ≤ 10^5
  • • nums2.length = nums1.length
  • • 0 ≤ nums1[i], nums2[i] ≤ 10^9
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“reorder nums1”
→ We choose which value faces which opponent, so this is a pairing problem.
“strictly bigger”
→ A win needs a strict beat. A tie is not a win.
“maximize the advantage”
→ Count the wins. A loss is not counted, but every value still has to face someone.
03

Approach Complexity Ladder

Brute Force — try every reorderingO(N · N!)O(N)

Try all orders of nums1 and keep the one with the most wins

Better — each opponent takes the smallest value that wins, in the given orderO(N²)O(N)

For each nums2 in its given order, scan the values left for the smallest one that beats it

Optimal — face the strongest opponent with our strongest value, or give up our weakestO(N log N)O(N)

Sort our values. Go through the opponents strongest first: our strongest wins if it can, otherwise our weakest is given up

Brute Force Intuition

Generate every way to reorder nums1. For each one, count the positions where our value beats nums2 at that position. Keep the reordering with the most wins.

Recognition rule for this sub-pattern:

Sort both sides and match the smallest that works, so bigger resources stay free.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.