1, 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.
1 class Solution { 2 public: 3 bool lemonadeChange(vector<int>& bills) { 4 int a = 0,b = 0; 5 for(int i = 0;i<bills.size();i++){ 6 if(bills[i] == 5) 7 a++; 8 if(bills[i] == 10){ 9 if(a == 0) 10 return false; 11 a--; 12 b++; 13 } 14 if(bills[i] == 20){ 15 if (a > 0 && b > 0){ 16 a--; 17 b--; 18 } 19 else if(a >= 3) 20 a -= 3; 21 else 22 return false; 23 24 } 25 } 26 return true; 27 28 } 29 };
2
Given the different denominations of coins coins and a total amount of amount. We can write a function to calculate the minimum number of coins needed to make up the total amount. If there is no combination can be composed of any of a total amount of coins, return -1.
Example 1:
Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/coin-change
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
1 #include<algorithm> 2 class Solution { 3 public: 4 int coinChange(vector<int>& coins, int amount) { 5 int max = amount + 1; 6 int dp[amount+1]; 7 for(int i = 0;i<amount+1;i++) 8 dp[i] = max; 9 dp[0] = 0; 10 for(int i = 1;i <= amount;i++) 11 for(int j = 0;j<coins.size();j++){ 12 if(coins[j] <= i) 13 dp[i] = min(dp[i],dp[i-coins[j]]+1); 14 } 15 return dp[amount]>amount?-1:dp[amount]; 16 } 17 };