본문 바로가기

Algorithm/Leet Code

LeetCode #860 LemonadeChange. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이..

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/11/02 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #7 MostExpensive. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, ..

2019/10/31 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #6 VolleyballResults. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면..

 

2019/10/20 - [Algorithm/Leet Code] - LeetCode #181 EmployeesEarningMoreTheirManagers. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면..

2019/10/20 - [Algorithm/Leet Code] - LeetCode #888 FairCandySwap. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/10/14 - [Algorithm/Leet Code] - LeetCode #182 DuplicateEmails. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베..

 

 

2019/08/14 - [Life/Item review] - Mi Band 4 review, 미밴드4 후기, 장점, 단점, 리뷰, 한글, global review, 미밴드4 글로벌 후기, 리뷰, 구매, 사용방법, setting, 세팅, ProsNCons

 

 

2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#

 

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법