Python program to find how much money will be earned by selling shoes

Suppose in a shoe shop there are n different shoes with different sizes present in an array called shoes and another list of pairs for m customers called demand is given, where demand[i] contains (shoe_size, money). A customer with demand i wants a shoe of size shoe_size and can pay the given amount of money. We need to find how much money the shopkeeper can earn by selling these shoes.

Example

If the input is shoes = [2,3,4,5,6,8,7,6,5,18] and demand = [(6,55), (6,45), (6,55), (4,40), (18,60), (10,50)], then the output will be 200 because ?

  • First customer buys shoe with size 6 at cost 55
  • Second customer buys shoe with size 6 at cost 45
  • Third customer cannot buy because there are no more shoes with size 6 in stock
  • Fourth customer buys shoe with size 4 at cost 40
  • Fifth customer buys shoe with size 18 at cost 60
  • Sixth customer cannot buy because there is no shoe of size 10

Total earnings: 55 + 45 + 40 + 60 = 200

Approach

To solve this problem, we will follow these steps ?

  • Create a frequency map of available shoe sizes using Counter
  • Initialize earnings to 0
  • For each customer demand:
    • Extract the required size and price they can pay
    • If the shoe size is available in stock:
      • Decrease the count of that shoe size by 1
      • Add the price to total earnings
  • Return total earnings

Implementation

from collections import Counter

def solve(shoes, demand):
    sizes = Counter(shoes)
    earn = 0
    
    for sz, price in demand:
        if sizes[sz] > 0:
            sizes[sz] -= 1
            earn += price
    
    return earn

# Test the function
shoes = [2, 3, 4, 5, 6, 8, 7, 6, 5, 18]
demand = [(6, 55), (6, 45), (6, 55), (4, 40), (18, 60), (10, 50)]

result = solve(shoes, demand)
print(f"Total earnings: {result}")
Total earnings: 200

How It Works

The Counter from collections module creates a frequency map of shoe sizes. For example, {2: 1, 3: 1, 4: 1, 5: 2, 6: 2, 7: 1, 8: 1, 18: 1} shows we have 2 shoes of size 6 and 2 shoes of size 5.

When processing each customer demand, we check if the requested size is available (sizes[sz] > 0). If yes, we sell the shoe by decrementing the count and adding the price to earnings.

Step-by-Step Execution

from collections import Counter

def solve_with_steps(shoes, demand):
    sizes = Counter(shoes)
    earn = 0
    
    print(f"Available shoes: {dict(sizes)}")
    print("Processing customers:")
    
    for i, (sz, price) in enumerate(demand, 1):
        if sizes[sz] > 0:
            sizes[sz] -= 1
            earn += price
            print(f"Customer {i}: Bought size {sz} for ${price}. Total: ${earn}")
        else:
            print(f"Customer {i}: Cannot buy size {sz} (out of stock)")
    
    return earn

shoes = [2, 3, 4, 5, 6, 8, 7, 6, 5, 18]
demand = [(6, 55), (6, 45), (6, 55), (4, 40), (18, 60), (10, 50)]

result = solve_with_steps(shoes, demand)
print(f"\nFinal earnings: ${result}")
Available shoes: {2: 1, 3: 1, 4: 1, 5: 2, 6: 2, 8: 1, 7: 1, 18: 1}
Processing customers:
Customer 1: Bought size 6 for $55. Total: $55
Customer 2: Bought size 6 for $45. Total: $100
Customer 3: Cannot buy size 6 (out of stock)
Customer 4: Bought size 4 for $40. Total: $140
Customer 5: Bought size 18 for $60. Total: $200
Customer 6: Cannot buy size 10 (out of stock)

Final earnings: $200

Conclusion

This solution uses a Counter to track shoe inventory and processes customer demands in order. The time complexity is O(n + m) where n is the number of shoes and m is the number of customers, making it an efficient approach for the shoe selling problem.

Updated on: 2026-03-26T15:32:49+05:30

480 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements