Program to count number of ways we can distribute coins to workers in Python

Suppose we have two lists of positive numbers called coins and salaries. Here coins[i] indicates the value for coin i and salaries[j] indicates the least amount of salary required to pay for worker j. We have one coin per type and must give each worker exactly one coin. We need to compute the number of ways to distribute coins to workers such that each worker receives a coin with value greater than or equal to their required salary.

Two ways are different if some worker receives one type of coin in one way but a different type of coin in the other way. If the result is very large, return result mod 10^9+7.

Example

If the input is coins = [1, 2, 3], salaries = [1, 2], then the output will be 4 ?

  • If we don't use coin with value 1: coins [2, 3] can pay both workers, giving us 2 ways
  • If we use coin with value 1: it can only go to worker 1 (salary 1), then either coin 2 or 3 can pay worker 2, giving us 2 more ways
  • Total: 4 ways

Algorithm

The approach uses binary search and combinatorics ?

  1. Sort both coins and salaries lists
  2. For each salary, find the first coin that can satisfy it using binary search
  3. Calculate the number of valid assignments using the formula based on available choices

Implementation

class Solution:
    def solve(self, coins, salaries):
        coins.sort()
        salaries.sort()
        num_coins = len(coins)
        num_salaries = len(salaries)
        dp = {}
        
        # Find the first valid coin for each salary using binary search
        for salary in salaries:
            l = 0
            r = num_coins - 1
            idx = num_coins
            
            while l <= r:
                m = l + (r - l) // 2
                if coins[m] >= salary:
                    idx = m
                    r = m - 1
                else:
                    l = m + 1
            
            # If no valid coin found, return 0
            if idx == num_coins:
                return 0
            dp[salary] = idx
        
        # Calculate number of ways
        result = 1
        for i in range(num_salaries - 1, -1, -1):
            salary = salaries[i]
            idx = dp[salary]
            # Available coins minus already assigned workers
            result *= (num_coins - idx) - (num_salaries - 1 - i)
        
        return result % (10**9 + 7)

# Test the solution
solution = Solution()
coins = [1, 2, 3]
salaries = [1, 2]
print(solution.solve(coins, salaries))
4

How It Works

The algorithm works in two phases ?

  1. Binary Search Phase: For each worker's salary requirement, find the smallest coin value that can satisfy it
  2. Counting Phase: Work backwards from the highest salary requirement, calculating available choices at each step

The key insight is that after sorting, we can greedily assign coins to workers with higher salary requirements first, as they have fewer valid coin options.

Time Complexity

The time complexity is O(n log m + n) where n is the number of workers and m is the number of coins. The binary search takes O(log m) for each of the n workers, and the final counting takes O(n).

Conclusion

This solution efficiently counts coin distribution ways using binary search to find valid coins and combinatorics to calculate possibilities. The key is processing workers in reverse salary order to maintain optimal assignment choices.

Updated on: 2026-03-25T11:32:27+05:30

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements