Ctrl K

A Rematch With the Same Lineup

Medium
Two teams meet for a rematch with identical rosters: the fighters' strengths are the numbers in powers. The opponent has already lined up in the order powers, so position i holds a fighter of strength powers[i]. You may line up your fighters (the same multiset of strengths) in any order. You win position i if your fighter there is strictly stronger than theirs. Return the largest number of wins you can get.
Example 1
powers=[4, 1, 4, 2, 7] → 3

Against [4, 1, 4, 2, 7] we field [7, 2, 1, 4, 4]: 7 > 4, 2 > 1 and 4 > 2 win, three in total. You cannot do better: only our single 7 can beat either of their 4s, and nobody beats their 7.

Example 2
powers=[3, 3, 3] → 0

All fighters are equally strong and a tie is not a win, so the answer is 0.

Example 3
powers=[5, 10, 15, 20] → 3
Constraints
  • 1 ≤ len(powers) ≤ 10⁵
  • 0 ≤ powers[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumFix the Middle of the Line MediumStick Triples That Form a Triangle MediumBest-Paid Gig for Each Worker MediumLining Up Against Rivals Medium
loading Python…
Input
powers=[4, 1, 4, 2, 7]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 88 tests on the server, hidden ones included.