MCPcopy Create free account
hub / github.com/ROUTINE-STUDY/Algorithm / Sanghoo

Class Sanghoo

LeetCode/Greedy/860. Lemonade Change/Sanghoo.java:6–44  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

4 * https://leetcode.com/problems/lemonade-change/
5 */
6public class Sanghoo {
7
8 // 무지성 조건문..
9 public static boolean lemonadeChange(int[] bills) {
10
11 if(bills[0] > 5) return false; // 첫 주문부터 10원권 이상을 내면 거슬러 줄 돈이 없어요,,
12 int[] moneys = new int[3]; // 돈을 담는 배열
13
14 for(int b : bills) {
15 if(b == 5) moneys[0]++; // 5원은 무조건 적립!
16 else if(b == 10) { // 10원
17 if(moneys[0] < 1) { // 5원이 한 장도 없다면 false
18 return false;
19 }
20 moneys[0]--;
21 moneys[1]++;
22 } else { // 20원
23 if(moneys[0] >= 1 && moneys[1] >= 1) { // 10원 5원 최소 한 장씩, 10원을 가지고 있다면 우선 소모 하기 위해 첫 번째 if문에 위치
24 moneys[0]--;
25 moneys[1]--;
26 moneys[2]++;
27 } else if(moneys[0] >= 3){ // 5원 최소 3장
28 moneys[0] -= 3;
29 moneys[2]++;
30 } else { // 거슬러 주지 못합니당..
31 return false;
32 }
33 }
34 }
35
36 return true;
37 }
38
39 public static void main(String[] args) {
40// int[] bills = {5,5,5,10,20};
41 int[] bills = {5,5,10,20,5,5,5,5,5,5,5,5,5,10,5,5,20,5,20,5};
42 System.out.println(lemonadeChange(bills));
43 }
44}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected