Skip to content

Latest commit

 

History

History
34 lines (30 loc) · 813 Bytes

File metadata and controls

34 lines (30 loc) · 813 Bytes

https://leetcode.com/problems/lemonade-change/description/

class Solution {
    public boolean lemonadeChange(int[] bills) {
        
        int fiveCount=0,tenCount=0;

        for(int bill:bills){

            if(bill==5){
                fiveCount++;
            } else if(bill==10){
                if(fiveCount>0){
                    fiveCount--;
                    tenCount++;
                } else {
                    return false;
                }
            } else {
                if(fiveCount>0 && tenCount>0){
                    tenCount--;
                    fiveCount--;
                } else if(fiveCount>2){
                    fiveCount-=3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}