본문 바로가기

Problem Solving/리트코드(leetcode)

Lemonade Change

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int bills5 = 0;
        int bills10 = 0;
        int bills20 = 0;
        for(int i = 0; i < bills.size(); i++) {
            if(bills[i] == 5) {
                bills5++;
            }
            else if(bills[i] == 10) {
                bills5--;
                bills10++;
            }
            else {
                if(bills10 > 0) {
                    bills10--;
                    bills5--;
                } else {
                    bills5 -= 3;
                    bills20++;
                }
            }
            if(bills5 < 0 || bills10 < 0) return false;
        }
        return true;
    }
};

'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글

Self Dividing Numbers  (0) 2022.01.16
Power of Four  (0) 2022.01.16
Add Digits  (0) 2022.01.16
Summary Ranges  (0) 2022.01.16
Single Number  (0) 2022.01.16