Ezvor

2838. Maximum Coins Heroes Can Collect

MediumArray Preparing judge… LeetCode
ArrayTwo PointersBinary SearchSortingPrefix Sum

Hints

Hint 1
If a hero can defeat the ith monster, then he defeats all the monsters having a power less than monster[i].
Hint 2
Sort monsters by their powers. Also change the order of the coins array according to this sort.
Hint 3
Construct a prefix sum array for the updated coins array.
Hint 4
For each hero, do a binary search and find the last position of the most powerful monster that this hero can defeat.
Hint 5
If said monster has index i, then the ith element of the partial sum array would be the answer.