- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

first one will buy shoe with size 6 at cost 55

second one will buy shoe with size 6 at cost 45

There is no shoe with size 6 in the stock

fourth one will buy shoe with size 4 at cost 40

fifth one will buy shoe with size 18 at cost 60

sixth one will not get shoe because there is no shoe of size 10

Total earn 55 + 45 + 40 + 60 = 200.

To solve this, we will follow these steps −

- n := size of demand
- sizes := a map containing frequencies of shoes based on size
- earn := 0
- for i in range 0 to n - 1, do
- (sz, price) := demand[i]
- if shoe with size sz is present in sizes, then
- sizes[sz] := sizes[sz] - 1
- earn := earn + price

- return earn

Let us see the following implementation to get better understanding

from collections import Counter def solve(shoes, demand): n = len(demand) sizes = Counter(shoes) earn = 0 for i in range(n): sz, price = demand[i] if sizes[sz]: sizes[sz] -= 1 earn += price 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)] print(solve(shoes, demand))

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

200

- Related Questions & Answers
- C++ Program to find out the maximum amount of money that can be made from selling cars
- C++ code to find money after buying and selling shares
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can make by holding and selling profit in Python
- How will be the world if currency(money) is removed from our lives?
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Program to find number of items left after selling n items in python
- Program to count number of switches that will be on after flipping by n persons in python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- How much money and time does a person need for an World Tour?
- How to maintain your favorite shoes?
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- How much should be a JavaScript Line Length?
- Set how much the item will grow relative to the rest of JavaScript.

Advertisements