Skip to content

Commit b8d0ea7

Browse files
committed
[E:50/531, M:52/969, H:6/386] add No: 890 Lemonade Change
1 parent 8e51d70 commit b8d0ea7

File tree

9 files changed

+303
-1
lines changed

9 files changed

+303
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## [柠檬水找零](https://leetcode-cn.com/problems/lemonade-change/)
2+
3+
在柠檬水摊上,每一杯柠檬水的售价为 `5` 美元。
4+
5+
顾客排队购买你的产品,(按账单 `bills` 支付的顺序)一次购买一杯。
6+
7+
每位顾客只买一杯柠檬水,然后向你付 `5` 美元、`10` 美元或 `20` 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 `5` 美元。
8+
9+
注意,一开始你手头没有任何零钱。
10+
11+
如果你能给每位顾客正确找零,返回 `true` ,否则返回 `false` 。
12+
13+
**示例 1:**
14+
15+
`**输入:**[5,5,5,10,20]
16+
**输出:**true
17+
**解释:** 前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。
18+
第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。
19+
第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。
20+
由于所有客户都得到了正确的找零,所以我们输出 true。
21+
`
22+
23+
**示例 2:**
24+
25+
`**输入:**[5,5,10]
26+
**输出:**true
27+
`
28+
29+
**示例 3:**
30+
31+
`**输入:**[10,10]
32+
**输出:**false
33+
`
34+
35+
**示例 4:**
36+
37+
`**输入:**[5,5,10,10,20]
38+
**输出:**false
39+
**解释:**
40+
前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。
41+
对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。
42+
对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。
43+
由于不是每位顾客都得到了正确的找零,所以答案是 false。
44+
`
45+
46+
 
47+
48+
**提示:**
49+
50+
* `0 <= bills.length <= 10000`
51+
* `bills[i]` 不是 `5` 就是 `10` 或是 `20` 
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/890/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[5,5,5,10,20]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input []int
23+
want bool
24+
}{
25+
{
26+
name: "test-[5,5,5,10,20]",
27+
input: []int{5, 5, 5, 10, 20},
28+
want: true,
29+
},
30+
{
31+
name: "test-[5,5,10]",
32+
input: []int{5, 5, 10},
33+
want: true,
34+
},
35+
{
36+
name: "test-[10,10]",
37+
input: []int{10, 10},
38+
want: false,
39+
},
40+
{
41+
name: "test-[5,5,10,10,20]",
42+
input: []int{5, 5, 10, 10, 20},
43+
want: false,
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
55+
solution.Export(test.input)
56+
cancel()
57+
})
58+
59+
if timeout {
60+
testLog.Fail(idx+1, test.name, "timeout")
61+
continue
62+
}
63+
64+
got := solution.Export(test.input)
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(bills []int) bool {
4+
return lemonadeChange(bills)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func lemonadeChange(bills []int) bool {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(bills []int) bool {
4+
return lemonadeChange(bills)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func lemonadeChange(bills []int) bool {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## [柠檬水找零](https://leetcode-cn.com/problems/lemonade-change/)
2+
3+
在柠檬水摊上,每一杯柠檬水的售价为 `5` 美元。
4+
5+
顾客排队购买你的产品,(按账单 `bills` 支付的顺序)一次购买一杯。
6+
7+
每位顾客只买一杯柠檬水,然后向你付 `5` 美元、`10` 美元或 `20` 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 `5` 美元。
8+
9+
注意,一开始你手头没有任何零钱。
10+
11+
如果你能给每位顾客正确找零,返回 `true` ,否则返回 `false` 。
12+
13+
**示例 1:**
14+
15+
`**输入:**[5,5,5,10,20]
16+
**输出:**true
17+
**解释:** 前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。
18+
第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。
19+
第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。
20+
由于所有客户都得到了正确的找零,所以我们输出 true。
21+
`
22+
23+
**示例 2:**
24+
25+
`**输入:**[5,5,10]
26+
**输出:**true
27+
`
28+
29+
**示例 3:**
30+
31+
`**输入:**[10,10]
32+
**输出:**false
33+
`
34+
35+
**示例 4:**
36+
37+
`**输入:**[5,5,10,10,20]
38+
**输出:**false
39+
**解释:**
40+
前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。
41+
对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。
42+
对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。
43+
由于不是每位顾客都得到了正确的找零,所以答案是 false。
44+
`
45+
46+
 
47+
48+
**提示:**
49+
50+
* `0 <= bills.length <= 10000`
51+
* `bills[i]` 不是 `5` 就是 `10` 或是 `20` 
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/890/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[5,5,5,10,20]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input []int
23+
want bool
24+
}{
25+
{
26+
name: "test-[5,5,5,10,20]",
27+
input: []int{5, 5, 5, 10, 20},
28+
want: true,
29+
},
30+
{
31+
name: "test-[5,5,10]",
32+
input: []int{5, 5, 10},
33+
want: true,
34+
},
35+
{
36+
name: "test-[10,10]",
37+
input: []int{10, 10},
38+
want: false,
39+
},
40+
{
41+
name: "test-[5,5,10,10,20]",
42+
input: []int{5, 5, 10, 10, 20},
43+
want: false,
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
55+
solution.Export(test.input)
56+
cancel()
57+
})
58+
59+
if timeout {
60+
testLog.Fail(idx+1, test.name, "timeout")
61+
continue
62+
}
63+
64+
got := solution.Export(test.input)
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(bills []int) bool {
4+
return lemonadeChange(bills)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func lemonadeChange(bills []int) bool {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(bills []int) bool {
4+
return lemonadeChange(bills)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func lemonadeChange(bills []int) bool {
13+
14+
}

0 commit comments

Comments
 (0)