860 Lemonade Change
- Greedy
- Easy
- 문제링크
풀이
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five_dollar = 0
ten_dollar = 0
result = True
# if the first order is $10, no change
if bills[0] == 10:
result = False
for i in range(len(bills)):
# if $5, increment five_dollar num
if bills[i] == 5:
five_dollar +=1
result = True
# if $10, check if five_dollar exists and increment ten_dollar num
elif bills[i] == 10:
if five_dollar >= 1:
ten_dollar +=1
five_dollar -=1
result = True
else:
result = False
break
# if $20, check if 1 ten_dollar + 1 five_dollar exists OR 3 five_dollar exists
elif bills[i] == 20:
if ten_dollar >= 1 and five_dollar >= 1:
ten_dollar -=1
five_dollar -=1
result = True
elif ten_dollar == 0 and five_dollar >= 3:
five_dollar -=3
result = True
else:
result = False
break
return result
결과
Runtime: 140 ms, faster than 63.35% of Python3 online submissions for Lemonade Change.
Memory Usage: 14.7 MB, less than 26.81% of Python3 online submissions for Lemonade Change.
'개발공부 > algorithm' 카테고리의 다른 글
[SWEA] 1226.미로 1 (dfs) (0) | 2021.02.27 |
---|---|
[리트코드] [Python] 118 Pascals Triangle - Binary search (0) | 2021.02.07 |
[BOJ] [Python] 백준 DP - 2748: 피보나치수2 (0) | 2020.10.28 |
[BOJ] [Python] 백준 자료구조 Dictionary - 7785: 회사에 있는 사람 (0) | 2020.10.27 |
[BOJ] [Python] 백준 자료구조 Stack - 1406: 에디터 (0) | 2020.10.16 |