-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlemon.py
More file actions
114 lines (96 loc) · 3.19 KB
/
lemon.py
File metadata and controls
114 lines (96 loc) · 3.19 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Solution(object):
def lemonadeChange(self, bills):
"""
:type bills: List[int]
:rtype: bool
"""
# Only 5 10 20 dollars based on the question
# Variables holding the count of all the denominations
five = 0
ten = 0
twenty = 0
# Iterating the customer payment
n = len(bills)
for i in range(n):
# Price of the lemonade is 5
change = bills[i] - 5
while change > 0:
print bills[i], change, five, ten
# Change containing 5 denomination
if change%10 != 0 and change%5 == 0:
if five > 0:
change -= 5
five -= 1
else:
return False
else:
# 20 doesnt matter, only 10 matters
if ten > 0:
change -= 10
ten -= 1
elif five > 0:
change -= 5
five -= 1
else:
return False
# Count Coins
if bills[i] == 5:
five += 1
elif bills[i] == 10:
ten += 1
else:
twenty += 1
if change == 0:
return True
else:
return False
"""
# Logic for every denomination
# Count everything
n = len(bills)
coins = {}
for i in range(n):
# Price of the lemonade is 5
change = bills[i] - 5
while change > 0:
#print change, coins
if change in coins and coins[change] > 0:
coins[change] -= 1
change -= change
elif change%5 == 0 and change%10 != 0:
if 5 in coins and coins[5] > 0:
coins[5] -= 1
change -= 5
else:
return False
else:
for k,v in coins.items():
if k <= change and v > 0:
change -= k
coins[k] -= 1
break
# Coins dictionary counting all the coins
if bills[i] not in coins:
coins[bills[i]] = 0
coins[bills[i]] += 1
if change == 0:
return True
else:
return False
"""
"""
# Scale case fail
n5 = 0
n = len(bills)
for i in range(n):
if bills[i] == 5:
n5 += 1
else:
while bills[i] > 5:
bills[i] -= 5
n5 -= 1
if n5 < 0:
return False
n5 += 1
return True
"""