Skip to content

Commit 2f97f49

Browse files
committed
Add solution and test-cases for problem 1352
1 parent 09a1dcb commit 2f97f49

File tree

3 files changed

+92
-25
lines changed

3 files changed

+92
-25
lines changed

leetcode/1301-1400/1352.Product-of-the-Last-K-Numbers/README.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [1352.Product of the Last K Numbers][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+
Design an algorithm that accepts a stream of integers and retrieves the product of the last `k` integers of the stream.
75

8-
**Example 1:**
6+
Implement the `ProductOfNumbers` class:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
- `ProductOfNumbers()` Initializes the object with an empty stream.
9+
- `void add(int num)` Appends the integer `num` to the stream.
10+
- `int getProduct(int k)` Returns the product of the last `k` numbers in the current list. You can assume that always the current list has at least `k` numbers.
1411

15-
## 题意
16-
> ...
12+
The test cases are generated so that, at any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
1713

18-
## 题解
14+
**Example 1:**
1915

20-
### 思路1
21-
> ...
22-
Product of the Last K Numbers
23-
```go
2416
```
25-
17+
Input
18+
["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
19+
[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
20+
21+
Output
22+
[null,null,null,null,null,null,20,40,0,null,32]
23+
24+
Explanation
25+
ProductOfNumbers productOfNumbers = new ProductOfNumbers();
26+
productOfNumbers.add(3); // [3]
27+
productOfNumbers.add(0); // [3,0]
28+
productOfNumbers.add(2); // [3,0,2]
29+
productOfNumbers.add(5); // [3,0,2,5]
30+
productOfNumbers.add(4); // [3,0,2,5,4]
31+
productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
32+
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
33+
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
34+
productOfNumbers.add(8); // [3,0,2,5,4,8]
35+
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
type ProductOfNumbers struct {
4+
data []int
5+
zeros map[int]struct{}
6+
}
7+
8+
func Constructor() ProductOfNumbers {
9+
return ProductOfNumbers{data: make([]int, 0), zeros: map[int]struct{}{}}
10+
}
11+
12+
func (this *ProductOfNumbers) Add(num int) {
13+
if num == 0 {
14+
this.data = append(this.data, num)
15+
this.zeros[len(this.data)-1] = struct{}{}
16+
return
17+
}
18+
if len(this.data) != 0 {
19+
if last := this.data[len(this.data)-1]; last != 0 {
20+
num *= last
21+
}
22+
}
23+
this.data = append(this.data, num)
24+
}
25+
26+
func (this *ProductOfNumbers) GetProduct(k int) int {
27+
end := len(this.data) - 1
28+
start := end - k
29+
for i := range this.zeros {
30+
if i > start && i <= end {
31+
return 0
32+
}
33+
}
34+
if start < 0 || this.data[start] == 0 {
35+
return this.data[end]
36+
}
37+
return this.data[end] / this.data[start]
38+
}
39+
40+
type op struct {
41+
name string
42+
v int
43+
}
44+
45+
func Solution(opts []op) []int {
46+
c := Constructor()
47+
ans := make([]int, 0)
48+
for _, o := range opts {
49+
if o.name == "a" {
50+
c.Add(o.v)
51+
continue
52+
}
53+
ans = append(ans, c.GetProduct(o.v))
54+
}
55+
return ans
556
}

leetcode/1301-1400/1352.Product-of-the-Last-K-Numbers/Solution_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []op
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []op{
17+
{"a", 3}, {"a", 0},
18+
{"a", 2},
19+
{"a", 5},
20+
{"a", 4},
21+
{"", 2}, {"", 3},
22+
{"", 4}, {"a", 8}, {"", 2},
23+
}, []int{20, 40, 0, 32}},
1924
}
2025

2126
// 开始测试
@@ -30,10 +35,10 @@ func TestSolution(t *testing.T) {
3035
}
3136
}
3237

33-
// 压力测试
38+
// 压力测试
3439
func BenchmarkSolution(b *testing.B) {
3540
}
3641

37-
// 使用案列
42+
// 使用案列
3843
func ExampleSolution() {
3944
}

0 commit comments

Comments
 (0)