LeetCode860 lemonade give change (greedy)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/No__names/article/details/102764603

Subject description:

In the lemonade stalls, each cup of lemonade for $ 5.

Customers line up to buy your product, (the bill in order to pay bills) to buy a drink.

Each customer only buy a cup of lemonade, then you pay $ 5, $ 10 or $ 20. You have the right to change for each customer, which means that net trade is each customer to pay you $ 5.

Note that, at the beginning you do not have any change.

If you give the correct change for each customer, returns true, otherwise false.

Example 1:

Enter: [5,5,5,10,20]
Output: true
explanation:
the top three customers there, we charge three five-dollar bill in order.
No. 4 customers there, we charge a ten dollar bill, and returned $ 5.
5th customer there, we also find a ten dollar bill and a five dollar bill.
Since all customers have been properly give change, so we output true.
Example 2:

Input: [5,5,10]
Output: true
Example 3:

Input: [10,10]
Output: false
Example 4:

Enter: [5,5,10,10,20]
output: false
explanation:
before two customers there, we charge two five-dollar bill in order.
For the next two customers, we charge a ten dollar bill, and then to return $ 5.
For the last customers, we can not return the $ 15, because we are now only two $ 10 bills.
Since not every customer get the right give change, so the answer is false.
 

prompt:

0 <= bills.length <= 10000
Bills [I] is not 10 or 20 5 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/lemonade-change
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

Ideas:

Greedy as possible first with a large denomination bill change, the reason can be explained: the hands of 10-dollar bills only when someone went to 20 dollars to find out, so you can find the time necessary to make the best use of $ 10, and the hands of the five-dollar bill in order to try to save the change to the customer's payment of $ 10. Giving top priority, and if the customer has to pay $ 50 to $ 20 that can only pay $ 50 a customer can come in handy, so to find someone to $ 20. (Of course, only three kinds of denominations of 5, 10 bills this question).

 

Code:

bool lemonadeChange(vector<int>& bills) {
	int money[2] = { 0 };//money[0]为5美元的张数,money[1]为10美元的张数
	if (bills.size() == 0)
		return true;
	if (bills[0] != 5)
		return false;
	for (int i = 0; i < bills.size(); i++) {
		if (bills[i] == 5)
			money[0]++;
		else if (bills[i] == 10) {
			//找零5块,存入一张10块
			if (money[0] > 0) {
				money[0]--;
				money[1]++;
			}
			else
				return false;
		}
		else if (bills[i] == 20) {
			//优先用10块找零
			if (money[1] > 0 && money[0] > 0) {
				money[0]--;
				money[1]--;
			}
			//没有10块找3张5块
			else if (money[0] >= 3) {
				money[0] -= 3;
			}
			else
				return false;
		}
	}
	return true;
}

 

Guess you like

Origin blog.csdn.net/No__names/article/details/102764603