Skip to content

Commit 5043238

Browse files
committed
Lemonade change solution added
1 parent c9f5b7a commit 5043238

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
---
2+
id: lemonade-change
3+
title: Lemonade Change
4+
sidebar_label: 0022 Lemonade Change
5+
tags:
6+
- Greedy Algorithm
7+
- JavaScript
8+
- TypeScript
9+
- Python
10+
- Java
11+
- C++
12+
description: "This document explores different approaches to solving the lemonade change problem, including an easy C++ solution with a greedy algorithm approach."
13+
---
14+
15+
## Problem
16+
17+
You are an owner of lemonade island, 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 given array `bills[]`). Each customer will only buy one lemonade and pay with either a \$5, \$10, or \$20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays \$5.
18+
19+
**Note:** At first, you do not have any bill to provide changes with. You can provide changes from the bills that you get from the previous customers.
20+
21+
Given an integer array bills of size N where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
```
28+
Input:
29+
N = 5
30+
bills[] = {5, 5, 5, 10, 20}
31+
32+
Output:
33+
True
34+
```
35+
36+
**Explanation:**
37+
From the first 3 customers, we collect three \$5 bills in order.
38+
From the fourth customer, we collect a \$10 bill and give back a \$5.
39+
From the fifth customer, we give a \$10 bill and a \$5 bill.
40+
Since all customers got correct change we return true.
41+
42+
**Example 2:**
43+
44+
```
45+
Input:
46+
N = 5
47+
bills[] = {5, 5, 10, 10, 20}
48+
49+
Output:
50+
False
51+
```
52+
53+
**Explanation:**
54+
From the first two customers in order, we collect two \$5 bills.
55+
For the next two customers in order, we collect a \$10 bill and give back a \$5 bill.
56+
For the last customer, we can not give the change of \$15 back because we only have two \$10 bills.
57+
Since not every customer received the correct change, the answer is false.
58+
59+
### Your Task
60+
You don't need to read input or print anything. Your task is to complete the function `lemonadeChange()` which takes the integer `N` and integer array `bills[]` as parameters and returns true if it is possible to provide change to every customer otherwise false.
61+
62+
**Expected Time Complexity:** O(N)
63+
**Expected Auxiliary Space:** O(1)
64+
65+
### Constraints
66+
- 1 ≤ N ≤ 10^5
67+
- bills[i] contains only {5, 10, 20}
68+
69+
## Solution
70+
71+
### Approach
72+
73+
We can solve this problem using a greedy algorithm. Here's a step-by-step approach:
74+
75+
1. Initialize two variables `five` and `ten` to keep count of the number of \$5 and \$10 bills available.
76+
2. Iterate through the bills array using a for loop.
77+
3. For each bill:
78+
- If the customer pays \$20, we need to return \$15. We first check if we have 1 * \$10 + 1 * \$5. If not, we check for 3 * \$5. If neither combination is available, we return false.
79+
- If the customer pays \$10, we need to return \$5. We check if we have any \$5 bills. If not, we return false.
80+
- If the customer pays \$5, we simply increase the count of \$5 bills.
81+
4. Finally, return true if we can provide the correct change for all customers.
82+
83+
### Implementation
84+
85+
<Tabs>
86+
<TabItem value="cpp" label="C++">
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
bool lemonadeChange(int n, vector<int> &bills) {
92+
int five = 0, ten = 0;
93+
for (int i = 0; i < n; i++) {
94+
if (bills[i] == 20) {
95+
if (ten && five) {
96+
ten--;
97+
five--;
98+
} else if (five >= 3) {
99+
five -= 3;
100+
} else {
101+
return false;
102+
}
103+
} else if (bills[i] == 10) {
104+
ten++;
105+
if (five) {
106+
five--;
107+
} else {
108+
return false;
109+
}
110+
} else {
111+
five++;
112+
}
113+
}
114+
return true;
115+
}
116+
};
117+
```
118+
119+
</TabItem>
120+
<TabItem value="javascript" label="JavaScript">
121+
122+
```javascript
123+
function lemonadeChange(bills) {
124+
let five = 0, ten = 0;
125+
for (let bill of bills) {
126+
if (bill === 20) {
127+
if (ten > 0 && five > 0) {
128+
ten--;
129+
five--;
130+
} else if (five >= 3) {
131+
five -= 3;
132+
} else {
133+
return false;
134+
}
135+
} else if (bill === 10) {
136+
ten++;
137+
if (five > 0) {
138+
five--;
139+
} else {
140+
return false;
141+
}
142+
} else {
143+
five++;
144+
}
145+
}
146+
return true;
147+
}
148+
```
149+
150+
</TabItem>
151+
<TabItem value="typescript" label="TypeScript">
152+
153+
```typescript
154+
function lemonadeChange(bills: number[]): boolean {
155+
let five = 0, ten = 0;
156+
for (let bill of bills) {
157+
if (bill === 20) {
158+
if (ten > 0 && five > 0) {
159+
ten--;
160+
five--;
161+
} else if (five >= 3) {
162+
five -= 3;
163+
} else {
164+
return false;
165+
}
166+
} else if (bill === 10) {
167+
ten++;
168+
if (five > 0) {
169+
five--;
170+
} else {
171+
return false;
172+
}
173+
} else {
174+
five++;
175+
}
176+
}
177+
return true;
178+
}
179+
```
180+
181+
</TabItem>
182+
<TabItem value="python" label="Python">
183+
184+
```python
185+
class Solution:
186+
def lemonadeChange(self, bills: List[int]) -> bool:
187+
five, ten = 0, 0
188+
for bill in bills:
189+
if bill == 20:
190+
if ten > 0 and five > 0:
191+
ten -= 1
192+
five -= 1
193+
elif five >= 3:
194+
five -= 3
195+
else:
196+
return False
197+
elif bill == 10:
198+
ten += 1
199+
if five > 0:
200+
five -= 1
201+
else:
202+
return False
203+
else:
204+
five += 1
205+
return True
206+
```
207+
208+
</TabItem>
209+
<TabItem value="java" label="Java">
210+
211+
```java
212+
class Solution {
213+
public boolean lemonadeChange(int[] bills) {
214+
int five = 0, ten = 0;
215+
for (int bill : bills) {
216+
if (bill == 20) {
217+
if (ten > 0 && five > 0) {
218+
ten--;
219+
five--;
220+
} else if (five >= 3) {
221+
five -= 3;
222+
} else {
223+
return false;
224+
}
225+
} else if (bill == 10) {
226+
ten++;
227+
if (five > 0) {
228+
five--;
229+
} else {
230+
return false;
231+
}
232+
} else {
233+
five++;
234+
}
235+
}
236+
return true;
237+
}
238+
}
239+
```
240+
241+
</TabItem>
242+
</Tabs>
243+
244+
### Complexity Analysis
245+
246+
- **Time Complexity:** O(N), where N is the length of the bills array. We iterate through the array once.
247+
- **Space Complexity:** O(1), as we only use a constant amount of extra space for variables `five` and `ten`.
248+
249+
---
250+
251+
## References
252+
253+
- **LeetCode Problem:** [Lemonade Change](https://www.geeksforgeeks.org/problems/lemonade-change/0)
254+
- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://www.geeksforgeeks.org/user/lakumvipwjge/)

0 commit comments

Comments
 (0)