714273. Max Coins

Medium

Max Coins

Slug: max-coins--170647 Difficulty: Medium Id: 714273 Topic Tags: Dynamic Programming, Greedy, Sorting, Algorithms Company Tags: None

Summary

The Max Coins problem involves finding the maximum number of coins that can be collected given a set of coin values and their corresponding frequencies. This problem is a classic example of a dynamic programming problem, requiring the use of greedy algorithms to solve efficiently.

Detailed Explanation

The problem can be broken down into the following steps:

  1. Initialize an array dp of size n, where n is the number of coin values.
  2. Sort the coin values in descending order and initialize a variable max_coins to 0.
  3. Iterate through the sorted coin values, for each value:
    • Calculate the maximum coins that can be collected with this coin using the formula: min(frequency, dp[previous_coin_value]), where frequency is the frequency of the current coin and dp[previous_coin_value] is the maximum coins collected so far.
    • Update max_coins to the maximum value calculated in step 3.1.
  4. Return max_coins.

Time complexity: O(n log n) due to sorting, where n is the number of coin values. Space complexity: O(n), where n is the number of coin values.

Optimized Solutions

Java

View on GeeksforGeeks
public int maxCoins(int[] coins) {
    Arrays.sort(coins);
    int max_coins = 0;
    for (int i = coins.length - 1; i >= 0; i--) {
        int freq = coins[i];
        if (freq > dp[coins[i-1]]) {
            dp[coins[i-1]] = freq;
            max_coins += freq;
        }
    }
    return max_coins;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.