본문 바로가기
Problem Solving/LeetCode

[LeetCode] 860. Lemonade Change - Java

by graycode 2026. 10. 27.

• 문제 링크

 

Lemonade Change - LeetCode

Can you solve this real interview question? Lemonade Change - 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

leetcode.com

 

• 풀이 코드

public class Solution {

    public boolean lemonadeChange(int[] bills) {
        int f = 0, t = 0;
        for (int i : bills) {
            if (i == 5) {
                f++;
            } else if (i == 10) {
                if (f == 0) return false;
                f--;
                t++;
            } else {
                if (f > 0 && t > 0) {
                    f--;
                    t--;
                } else if (f >= 3) f -= 3;
                else return false;
            }
        }

        return true;
    }

}

댓글