Advantage Shuffle - 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.
Input & Output
Example 1 — Basic Advantage
$
Input:
nums1 = [2,7,11,15], nums2 = [1,10,4,11]
›
Output:
[2,11,7,15]
💡 Note:
We rearrange nums1 to [2,11,7,15]. Comparing with nums2=[1,10,4,11]: 2>1 ✓, 11>10 ✓, 7>4 ✓, 15>11 ✓. All 4 positions give advantage.
Example 2 — Partial Advantage
$
Input:
nums1 = [12,24,8,32], nums2 = [13,25,32,11]
›
Output:
[24,32,8,12]
💡 Note:
Optimal arrangement: 24>13 ✓, 32>25 ✓, 8≤32 ✗, 12>11 ✓. We get 3 advantages out of 4 possible.
Example 3 — Minimum Case
$
Input:
nums1 = [3,5], nums2 = [6,4]
›
Output:
[5,3]
💡 Note:
With 2 elements: 5≤6 ✗, 3≤4 ✗. No advantage possible, but [5,3] is one valid arrangement.
Constraints
- 1 ≤ nums1.length ≤ 105
- nums2.length == nums1.length
- 0 ≤ nums1[i], nums2[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
nums1=[2,7,11,15] vs nums2=[1,10,4,11]
2
Find Optimal Arrangement
Use greedy strategy to maximize advantages
3
Result
Rearranged nums1=[2,11,7,15] achieves 4 wins
Key Takeaway
🎯 Key Insight: Use greedy strategy - assign the smallest element that can win, or waste the smallest if no win is possible
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code