Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2838: Maximum Coins Heroes Can Collect

In this guide, we solve Leetcode #2838 Maximum Coins Heroes Can Collect in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

There is a battle and n heroes are trying to defeat m monsters. You are given two 1-indexed arrays of positive integers heroes and monsters of length n and m, respectively.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Array, Two Pointers, Binary Search, Prefix Sum, Sorting

Intuition

The constraints hint that we can reason about two ends of the data at once, which is perfect for a two-pointer scan.

Moving one pointer at a time keeps the invariant intact and avoids nested loops.

Approach

Place pointers at the left and right ends and move them based on the comparison or target condition.

This yields a clean linear pass after any required sorting.

Steps:

  • Set left and right pointers.
  • Move a pointer based on the condition.
  • Update the best answer while scanning.

Example

Input: heroes = [1,4,2], monsters = [1,1,5,2,3], coins = [2,3,4,5,6] Output: [5,16,10] Explanation: For each hero, we list the index of all the monsters he can defeat: 1st hero: [1,2] since the power of this hero is 1 and monsters[1], monsters[2] <= 1. So this hero collects coins[1] + coins[2] = 5 coins. 2nd hero: [1,2,4,5] since the power of this hero is 4 and monsters[1], monsters[2], monsters[4], monsters[5] <= 4. So this hero collects coins[1] + coins[2] + coins[4] + coins[5] = 16 coins. 3rd hero: [1,2,4] since the power of this hero is 2 and monsters[1], monsters[2], monsters[4] <= 2. So this hero collects coins[1] + coins[2] + coins[4] = 10 coins. So the answer would be [5,16,10].

Python Solution

class Solution: def maximumCoins( self, heroes: List[int], monsters: List[int], coins: List[int] ) -> List[int]: m = len(monsters) idx = sorted(range(m), key=lambda i: monsters[i]) s = list(accumulate((coins[i] for i in idx), initial=0)) ans = [] for h in heroes: i = bisect_right(idx, h, key=lambda i: monsters[i]) ans.append(s[i]) return ans

Complexity

The time complexity is O((m+n)×log⁡n)O((m + n) \times \log n)O((m+n)×logn), and the space complexity is O(m)O(m)O(m). The space complexity is O(m)O(m)O(m).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy