forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemonade-change.py
54 lines (48 loc) · 1.3 KB
/
lemonade-change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Time: O(n)
# Space: O(1)
import collections
class Solution(object):
def lemonadeChange(self, bills):
"""
:type bills: List[int]
:rtype: bool
"""
coins = [20, 10, 5]
counts = collections.defaultdict(int)
for bill in bills:
counts[bill] += 1
change = bill - coins[-1]
for coin in coins:
if change == 0:
break
if change >= coin:
count = min(counts[coin], change//coin)
counts[coin] -= count
change -= coin * count
if change != 0:
return False
return True
class Solution2(object):
def lemonadeChange(self, bills):
"""
:type bills: List[int]
:rtype: bool
"""
five, ten = 0, 0
for bill in bills:
if bill == 5:
five += 1
elif bill == 10:
if not five:
return False
five -= 1
ten += 1
else:
if ten and five:
ten -= 1
five -= 1
elif five >= 3:
five -= 3
else:
return False
return True