Skip to content

Commit 56cfbe8

Browse files
authored
Merge pull request #913 from 0xff-dev/1052
Add solution and test-cases for problem 1052
2 parents 436b35e + 4750910 commit 56cfbe8

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

leetcode/1001-1100/1052.Grumpy-Bookstore-Owner/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [1052.Grumpy Bookstore Owner][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a bookstore owner that has a store open for `n` minutes. Every minute, some number of customers enter the store. You are given an integer array `customers` of length `n` where `customers[i]` is the number of the customer that enters the store at the start of the i<sup>th</sup> minute and all those customers leave after the end of that minute.
5+
6+
On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where `grumpy[i]` is `1` if the bookstore owner is grumpy during the i<sup>th</sup> minute, and is `0` otherwise.
7+
8+
When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.
9+
10+
The bookstore owner knows a secret technique to keep themselves not grumpy for `minutes` consecutive minutes, but can only use it once.
11+
12+
Return the maximum number of customers that can be satisfied throughout the day.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
18+
Output: 16
19+
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
20+
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
1321
```
1422

15-
## 题意
16-
> ...
17-
18-
## 题解
23+
**Example 2:**
1924

20-
### 思路1
21-
> ...
22-
Grumpy Bookstore Owner
23-
```go
2425
```
25-
26+
Input: customers = [1], grumpy = [0], minutes = 1
27+
Output: 1
28+
```
2629

2730
## 结语
2831

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(customers []int, grumpy []int, minutes int) int {
4+
a := make([]int, len(customers))
5+
6+
pre := 0
7+
total := 0
8+
9+
for i := 0; i < len(customers); i++ {
10+
if grumpy[i] == 1 {
11+
pre += customers[i]
12+
} else {
13+
total += customers[i]
14+
}
15+
a[i] = pre
16+
}
17+
18+
// 1,0,1,2,1,1,7,5
19+
// 0,1,0,1,0,1,0,1
20+
ans := 0
21+
for end := minutes - 1; end < len(customers); end++ {
22+
start := end + 1 - minutes
23+
get := a[end]
24+
if start > 0 {
25+
get -= a[start-1]
26+
}
27+
28+
ans = max(ans, total+get)
29+
30+
}
31+
32+
return ans
533
}

leetcode/1001-1100/1052.Grumpy-Bookstore-Owner/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
c, g []int
14+
m int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 0, 1, 2, 1, 1, 7, 5}, []int{0, 1, 0, 1, 0, 1, 0, 1}, 3, 16},
18+
{"TestCase2", []int{1}, []int{0}, 1, 1},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.c, c.g, c.m)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.c, c.g, c.m)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)