LeetCode #860 LemonadeChange. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이스, sql, query, 쿼리
Runtime: 16 ms, faster than 77.87% of C++ online submissions for Lemonade Change.
Memory Usage: 9.6 MB, less than 100.00% of C++ online submissions for Lemonade Change.
LeetCode #860
Q.
At a lemonade stand, each lemonade costs $5.
Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).
Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.
Note that you don't have any change in hand at first.
Return true if and only if you can provide every customer with correct change.
레모네이드 가판대에, 레몬에이드는 5달러이다.
손님들은 사려고 줄을 서있고, 한번에 한개씩만 주문할 수 있다. (순서는 지폐로 나타내어진다)
각 손님들은 하나의 레몬에이드를 사고, 5, 10, 20 달러 지폐를 낸다. 너는 만드시 각 손님들한테 옳은 잔돈을 줘야한다. 손님들이 5달러의 거래를 하기 위해서.
처음에는 잔돈이 하나도 없다.
모든 손님들한테 잔돈을 제대로 줄 수 있는지를 true/false 로 반환해라.
Example 1:
Input: [5,5,5,10,20]
Output: true
Explanation: From the first 3 customers, we collect three $5 bills in order. From the fourth customer, we collect a $10 bill and give back a $5. From the fifth customer, we give a $10 bill and a $5 bill. Since all customers got correct change, we output true.
Example 2:
Input: [5,5,10]
Output: true
Example 3:
Input: [10,10]
Output: false
Example 4:
Input: [5,5,10,10,20]
Output: false E
xplanation: From the first two customers in order, we collect two $5 bills. For the next two customers in order, we collect a $10 bill and give back a $5 bill. For the last customer, we can't give change of $15 back because we only have two $10 bills. Since not every customer received correct change, the answer is false.
Note:
- 0 <= bills.length <= 10000
- bills[i] will be either 5, 10, or 20.
Process
/ Process
//1. Input customer bills
//2. Make change bill dynamicPlate (5, 10)
//3. Iterate bills from begin to the end
// 3.1. Check the change comparing bills
//4. Return result
// 처리과정
//1. bills 입력받는다.
//2. 잔돈 다이나믹플레이트 만들어둔다. (5원 10원)
//3. 입력 bills 전체 반복한다.
// 3.1. 잔돈 거를러주며 최신화 시킨다.
//4. 결과값 반환한다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
bool result = true;
vector<int> changePlate;
changePlate.push_back(0);
changePlate.push_back(0);
for (int i = 0; result && i < bills.size(); ++i) {
if (bills[i] > 5) {
if (bills[i] > 10) {
if (changePlate[1] > 0) {
--changePlate[0];
--changePlate[1];
} else {
changePlate[0] -= 3;
}
} else {
--changePlate[0];
++changePlate[1];
}
} else {
++changePlate[0];
}
if (changePlate[0] < 0 || changePlate[1] < 0) {
result = false;
}
}
return result;
}
};
Something else you might like...?
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법