↓Skip to main content
  1. LeetCode/

LeetCode 2838: Maximum Coins Heroes Can Collect

·2 mins· ·
LeetCode Medium Binary-Search Prefix-Sum Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-10

  • Submissions: Accepted (first try)

Approach
#

A hero beats every monster with power <= their own and collects all of their coins. So each hero’s answer is “the total coins of monsters with power <= the hero”.

  1. Sort (monster power, coins) by power.
  2. Build prefix sums of the coins.
  3. For each hero, bisect_right gives the index idx of the first monster they cannot beat; the answer is prefix[idx].

My Solution
#

import bisect

class Solution:
    def maximumCoins(self, heroes: List[int], monsters: List[int], coins: List[int]) -> List[int]:
        pairs = sorted(zip(monsters, coins))
        powers = [p for p, _ in pairs]
        prefix = [0]
        for _, c in pairs:
            prefix.append(prefix[-1] + c)
        return [prefix[bisect.bisect_right(powers, h)] for h in heroes]

bisect_right is used because monsters with power equal to the hero’s can be beaten too.

Complexity
#

Time O((n + m) log m), space O(m).

Mistakes to Avoid
#

  • Using bisect_left: monsters with exactly equal power get skipped.
  • Re-summing for every hero: O(n * m).
  • Sorting monsters and coins separately: the pairing breaks, so zip them before sorting.

Interview Explanation
#

Each hero’s answer is the total coins of monsters whose power does not exceed theirs. I sort monsters together with their coins by power, build coin prefix sums, and for each hero use bisect_right to find the first monster they cannot beat; the prefix sum there is the answer. Time O((n + m) log m).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3814: Maximum Capacity Within Budget
·2 mins
LeetCode Medium Binary-Search Sorting Prefix-Sum
LeetCode 3814 note: sort by cost with a prefix maximum of capacity, and bisect the most expensive partner for each machine
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2054: Two Best Non-Overlapping Events
·2 mins
LeetCode Medium Dynamic-Programming Binary-Search Sorting Heap
LeetCode 2054 note: with at most two events, pair each event with the most valuable event that ends before it starts; sort by end time and use a prefix max with binary search
LeetCode 3323: Minimize Connected Groups by Inserting Interval
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3323 note: merge first; a new interval can join groups l through r ⇔ start of group r − end of group l <= k, so a sliding window finds the most groups one interval can connect