Premium problem151. Top-N Rows per Category

Hard Locked

Given category (1-D int), revenue (1-D float) and integer n, return the indices of the top-n revenue rows within each category:

  • order by category ascending, then revenue descending
  • a category with fewer than n rows contributes all of theirs

Input

category = [0 0 0 1 1]
revenue = [5. 9. 7. 2. 8.]
n = 2

Output

[1 2 4 3]

Premium problem

This one's part of Premium. Unlock the full NumPy track plus every other premium problem on the site.

Implement solve(...)