Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5cb780f

Browse files
committedJun 4, 2019
feat(860): finish this
1 parent f70916b commit 5cb780f

File tree

4 files changed

+137
-8
lines changed

4 files changed

+137
-8
lines changed
 

‎README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88
## 刷题原则
99

1010
- 广度优先
11-
- 人的注意力只有半小时
11+
- 人的注意力只有半小时,所以
1212
- 测试覆盖率 100%
1313

14-
## first
15-
16-
- 不知道特定的算法,数据结构
17-
- 没有正确的思维方式
18-
- 不知道最优解在哪里想
19-
- 做题没有任何感觉,代码不知道从哪里写起
20-
2114
## vscode-debug-for-golang
2215

2316
```json
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# [860. Lemonade Change](https://leetcode.com/problems/lemonade-change/)
2+
3+
## 2019/06/05
4+
5+
### 题目 💗[easy]
6+
7+
At a lemonade stand, each lemonade costs `$5`.
8+
9+
Customers are standing in a queue to buy from you, and order one at a time (in the order specified by `bills`).
10+
11+
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`.
12+
13+
Note that you don't have any change in hand at first.
14+
15+
Return true if and only if you can provide every customer with correct change.
16+
17+
Example 1:
18+
19+
```bash
20+
Input: [5,5,5,10,20]
21+
Output: true
22+
Explanation:
23+
From the first 3 customers, we collect three $5 bills in order.
24+
From the fourth customer, we collect a $10 bill and give back a $5.
25+
From the fifth customer, we give a $10 bill and a $5 bill.
26+
Since all customers got correct change, we output true.
27+
```
28+
29+
Example 2:
30+
31+
```bash
32+
Input: [5,5,10]
33+
Output: true
34+
```
35+
36+
Example 3:
37+
38+
```bash
39+
Input: [10,10]
40+
Output: false
41+
```
42+
43+
Example 4:
44+
45+
```bash
46+
Input: [5,5,10,10,20]
47+
Output: false
48+
Explanation:
49+
From the first two customers in order, we collect two $5 bills.
50+
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
51+
For the last customer, we can't give change of $15 back because we only have two $10 bills.
52+
Since not every customer received correct change, the answer is false.
53+
```
54+
55+
Note:
56+
57+
1. 0 <= bills.length <= 10000
58+
2. bills[i] will be either 5, 10, or 20.

‎algorithm/860.LemonadeChange/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lemonadeChange
2+
3+
func lemonadeChange(bills []int) bool {
4+
five := 0
5+
ten := 0
6+
for _, e := range bills {
7+
switch e {
8+
case 5:
9+
five++
10+
case 10:
11+
if five > 0 {
12+
// 付得起
13+
five--
14+
ten++
15+
} else {
16+
return false
17+
}
18+
case 20:
19+
if ten > 0 && five > 0 {
20+
ten--
21+
five--
22+
} else if five > 2 {
23+
five -= 3
24+
} else {
25+
return false
26+
}
27+
}
28+
}
29+
return true
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package lemonadeChange
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var tcs = []struct {
10+
N1 []int
11+
ans bool
12+
}{
13+
{
14+
[]int{5, 5, 5, 10, 20},
15+
true,
16+
},
17+
{
18+
[]int{5, 5, 10},
19+
true,
20+
},
21+
{
22+
[]int{10, 10},
23+
false,
24+
},
25+
{
26+
[]int{5, 5, 10, 10, 20},
27+
false,
28+
},
29+
{
30+
[]int{5, 5, 5, 5, 20, 20, 5, 5, 20, 5},
31+
false,
32+
},
33+
{
34+
[]int{5, 5, 10, 20, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 5, 5, 20, 5, 20, 5},
35+
true,
36+
},
37+
{
38+
[]int{5, 5, 5, 20, 5, 5, 5, 20, 5, 5, 5, 10, 5, 20, 10, 20, 10, 20, 5, 5},
39+
false,
40+
},
41+
}
42+
43+
func Test_bitwiseComplement(t *testing.T) {
44+
ast := assert.New(t)
45+
for _, tc := range tcs {
46+
ast.Equal(tc.ans, lemonadeChange(tc.N1), "输入:%v", tc)
47+
}
48+
}

0 commit comments

Comments
 (0)
Failed to load comments.