Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1052 #913

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions leetcode/1001-1100/1052.Grumpy-Bookstore-Owner/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# [1052.Grumpy Bookstore Owner][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

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.

When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.

The bookstore owner knows a secret technique to keep themselves not grumpy for `minutes` consecutive minutes, but can only use it once.

Return the maximum number of customers that can be satisfied throughout the day.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Grumpy Bookstore Owner
```go
```

Input: customers = [1], grumpy = [0], minutes = 1
Output: 1
```

## 结语

Expand Down
32 changes: 30 additions & 2 deletions leetcode/1001-1100/1052.Grumpy-Bookstore-Owner/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(customers []int, grumpy []int, minutes int) int {
a := make([]int, len(customers))

pre := 0
total := 0

for i := 0; i < len(customers); i++ {
if grumpy[i] == 1 {
pre += customers[i]
} else {
total += customers[i]
}
a[i] = pre
}

// 1,0,1,2,1,1,7,5
// 0,1,0,1,0,1,0,1
ans := 0
for end := minutes - 1; end < len(customers); end++ {
start := end + 1 - minutes
get := a[end]
if start > 0 {
get -= a[start-1]
}

ans = max(ans, total+get)

}

return ans
}
20 changes: 10 additions & 10 deletions leetcode/1001-1100/1052.Grumpy-Bookstore-Owner/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
c, g []int
m int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 0, 1, 2, 1, 1, 7, 5}, []int{0, 1, 0, 1, 0, 1, 0, 1}, 3, 16},
{"TestCase2", []int{1}, []int{0}, 1, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.c, c.g, c.m)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.c, c.g, c.m)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading