Maximum Coins Heroes Can Collect - Problem

โš”๏ธ Maximum Coins Heroes Can Collect

Imagine an epic battle where n brave heroes face off against m fearsome monsters to collect precious coins! Each hero has a specific power level, and each monster guards a treasure of coins.

The Rules:

  • A hero can defeat a monster only if the hero's power is greater than or equal to the monster's power
  • Each hero can defeat multiple monsters, but can only defeat each monster once
  • Heroes don't lose health after battles - they can keep fighting!
  • Multiple heroes can defeat the same monster (but each hero gets coins only once per monster)

Your Goal: For each hero, calculate the maximum coins they can collect by strategically choosing which monsters to defeat.

Input:

  • heroes[i] - power of the i-th hero
  • monsters[j] - power of the j-th monster
  • coins[j] - coins earned for defeating the j-th monster

Output: An array where result[i] is the maximum coins the i-th hero can collect.

Input & Output

example_1.py โ€” Basic Test Case
$ Input: heroes = [1, 4, 2], monsters = [1, 2, 3], coins = [1, 2, 3]
โ€บ Output: [1, 6, 3]
๐Ÿ’ก Note: Hero 1 (power=1) can defeat monster 1 (power=1) โ†’ 1 coin. Hero 2 (power=4) can defeat all monsters (1,2,3) โ†’ 1+2+3 = 6 coins. Hero 3 (power=2) can defeat monsters 1,2 โ†’ 1+2 = 3 coins.
example_2.py โ€” Heroes with Same Power
$ Input: heroes = [5, 5], monsters = [2, 3, 7], coins = [1, 2, 4]
โ€บ Output: [3, 3]
๐Ÿ’ก Note: Both heroes have power 5, so both can defeat monsters with power 2 and 3, earning 1+2 = 3 coins each. Neither can defeat the monster with power 7.
example_3.py โ€” Edge Case - Weak Heroes
$ Input: heroes = [1, 1], monsters = [5, 10], coins = [10, 20]
โ€บ Output: [0, 0]
๐Ÿ’ก Note: Both heroes have power 1, but all monsters have higher power (5, 10), so no hero can defeat any monster. Each hero collects 0 coins.

Constraints

  • 1 โ‰ค n, m โ‰ค 105
  • 1 โ‰ค heroes[i], monsters[i], coins[i] โ‰ค 105
  • All arrays are 0-indexed in implementation

Visualization

Tap to expand
โš”๏ธ Heroes vs Monsters: Optimal StrategyBefore Optimization:Hero 1M3M1M2O(nร—m) comparisonsAfter Optimization:Step 1: Sort monsters by powerM1M2M3Step 2: Build prefix sum array05815Step 3: Binary search for each heroHeroBinary SearchO(log m)๐Ÿง  Algorithm Breakdown1. Sort Monsters by Power:โ€ข Create (power, coins) pairsโ€ข Sort by power: O(m log m)2. Build Prefix Sum:โ€ข prefix[i] = sum of coins[0..i]โ€ข Enables O(1) range sum queries3. For Each Hero:โ€ข Binary search: find rightmost monster with power โ‰ค hero_powerโ€ข Answer = prefix[found_index]Complexity Analysis:โ€ข Time: O(m log m + n log m)โ€ข Space: O(m)Why It Works:โœ“ Sorting allows binary searchโœ“ Prefix sum gives instant totalsโœ“ Each hero gets maximum coinsโœ“ Much faster than brute force๐Ÿ’ก Key InsightSorting + Binary Search + Prefix Sumtransforms O(nร—m) into O(n log m)!๐ŸŽฏ This optimization makes the solution scalable for large inputs!
Understanding the Visualization
1
Assess the Battlefield
Heroes survey all monsters and their rewards
2
Sort by Difficulty
Arrange monsters from weakest to strongest for strategic planning
3
Calculate Cumulative Rewards
Build a prefix sum to know total coins up to any point
4
Find Each Hero's Limit
Use binary search to find the strongest monster each hero can defeat
5
Collect Maximum Coins
Each hero collects coins from all defeatable monsters
Key Takeaway
๐ŸŽฏ Key Insight: By sorting monsters and using binary search with prefix sums, we transform an O(nร—m) brute force solution into an efficient O((n+m) log m) algorithm that scales beautifully!
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 31
52.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen