Skip to content

Commit d8ca883

Browse files
authored
Merge pull request #823 from 0xff-dev/713
Add solution and test-cases for problem 713
2 parents 28f59a6 + 6957c22 commit d8ca883

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

leetcode/701-800/0713.Subarray-Product-Less-Than-K/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# [713.Subarray Product Less Than K][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+
Given an array of integers `nums` and an integer `k`, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than `k`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums = [10,5,2,6], k = 100
10+
Output: 8
11+
Explanation: The 8 subarrays that have product less than 100 are:
12+
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
13+
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
1314
```
1415

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

20-
### 思路1
21-
> ...
22-
Subarray Product Less Than K
23-
```go
2418
```
25-
19+
Input: nums = [1,2,3], k = 0
20+
Output: 0
21+
```
2622

2723
## 结语
2824

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
cache := make(map[int]int)
5+
ans := 0
6+
if nums[0] < k {
7+
cache[nums[0]] = 1
8+
ans++
9+
}
10+
for i := 1; i < len(nums); i++ {
11+
if nums[i] >= k {
12+
cache = map[int]int{}
13+
continue
14+
}
15+
next := map[int]int{nums[i]: 1}
16+
for kk, v := range cache {
17+
if r := nums[i] * kk; r < k {
18+
next[r] += v
19+
}
20+
}
21+
cache = next
22+
for _, v := range cache {
23+
ans += v
24+
}
25+
}
26+
27+
return ans
528
}

leetcode/701-800/0713.Subarray-Product-Less-Than-K/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+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{10, 5, 2, 6}, 100, 8},
18+
{"TestCase2", []int{1, 2, 3}, 0, 0},
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.nums, c.k)
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",
27+
c.expect, got, c.nums, c.k)
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)