557. Maximum Coins Heroes Can Collect
In the combat, n heroes are attempting to vanquish m monsters. Two 1-indexed arrays of heroes and monsters of lengths n and m, respectively, are shown to you. Monsters[i] is the power of a monster, while heroes[i] is the power of a hero.
- If monsters[j] <= heroes[i], then the jth monster can be vanquished by the ith hero.
Additionally, a 1-indexed array of positive integer coins of length m is provided to you. The quantity of money that each hero receives upon beating the ith monster is shown by coins[i].
The maximum amount of coins that the ith hero can gather from this battle is ans[i], which should return an array ans of length n.
Example 1:
Input : heroes = [1, 2, 3] , monsters = [1, 2, 1, 3, 5] , coins = [1, 2, 3, 4, 5]
Output : [4, 6, 10]
Explanation :
For each hero, we list the index of all the monsters he can defeat:
1st hero: [1,3] since the power of this hero is 1 and monsters[1], monsters[3] <= 1. So this hero collects coins[1] + coins[3] = 4 coins.
2nd hero: [1,2,3] since the power of this hero is 2 and monsters[1], monsters[2], monsters[3] <= 2. So this hero collects coins[1] + coins[2] + coins[3] = 6 coins.
3rd hero: [1,2,3,4] since the power of this hero is 3 and monsters[1], monsters[2], monsters[3], monsters[4] <= 3. So this hero collects coins[1] + coins[2] + coins[3] + coins[4] = 10 coins.
So the answer would be [4,6,10].
Example 2:
Input : heroes = [5, 6, 7] , monsters = [1, 15, 1, 3, 6] , coins = [1, 2, 3, 4, 5]
Output : [8, 13, 13]
Explanation :
For each hero, we list the index of all the monsters he can defeat:
1st hero: [1,3,4] since the power of this hero is 5 and monsters[1], monsters[3], monsters[4] <= 5. So this hero collects coins[1] + coins[3] + coins[4] = 8 coins.
2nd hero: [1,3,4,5] since the power of this hero is 6 and monsters[1], monsters[3], monsters[4], monsters[5] <= 6. So this hero collects coins[1] + coins[3] + coins[4] + coins[5] = 13 coins.
3d hero: [1,3,4,5] since the power of this hero is 7 and monsters[1], monsters[3], monsters[4], monsters[5] <= 7. So this hero collects coins[1] + coins[3] + coins[4] + coins[5] = 13 coins.
So the answer would be [8, 13, 13].
Now Your Turn!
Pick the correct output for the given inputInput : heroes = [25, 6, 7] , monsters = [1, 15, 1, 3, 6] , coins = [1, 2, 3, 4, 5]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n == heroes.length <= 105
- 1 <= m == monsters.length <= 105
- coins.length == m
- 1 <= heroes[i], monsters[i], coins[i] <= 109