- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Buying Cars in Python
Suppose we have a list of prices of cars for sale, and we also have a budget k, we have to find the maximum number of cars we can buy.
So, if the input is like [80, 20, 10, 30, 80], k = 85, then the output will be 3 as we can buy three cars with prices 20, 10, 40
To solve this, we will follow these steps −
count := 0
sort the list prices
for i in range 0 to size of prices, do
if prices[i] <=k, then
k := k-prices[i]
count := count + 1
come out from the loop
Let us see the following implementation to get better understanding −
class Solution: def solve(self, prices, k): count =0 prices.sort() for i in range(len(prices)): if(prices[i]<=k): k = k-prices[i] count += 1 else: break return count ob = Solution() p = [80, 20, 10, 30, 80] print(ob.solve(p, 85))
[80, 20, 10, 30, 80], 85
- What are the upcoming cars in 2018?
- What are the top 5 fastest cars in the world?
- What products of Patanjali are worth buying?
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Maximum profit after buying and selling the stocks in C++
- Program to find maximum profit after buying and selling stocks at most two times in python
- What to look for before buying a used car?
- Which are the trustworthy brands for buying sanitary pads?
- Program to find the maximum profit we can get by buying on stock market once in Python
- Do you think ban on diesel cars in delhi would effect sales in other cities?
- Is buying home appliances from home shop channels worth it?
- C++ code to find money after buying and selling shares
- Program to find the maximum profit we can get by buying on stock market multiple times in Python
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Maximum profit by buying and selling a share at most twice