Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7b8505

Browse files
authored
713 subarray product less than k solved. (#19)
* 713 subarray product less than k solved. * add more ut case for spltk.go
1 parent 6064ffe commit e7b8505

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ continually updating 😃.
2626
* [350. Intersection of Two Arrays II](./src/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)   *`hash table`*
2727
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`*
2828
* [454. 4Sum II](./src/0454_4sum2/4sum2.go)   *`hash table`*
29+
* [713. Subarray Product Less Than K](src/0713_subarray_product_less_than_k/spltk.go)   *`sliding window`*  *`array`*
2930
* [747. Largest Number At Least Twice of Others](./src/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)
3031

3132
### String
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
713. Subarray Product Less Than K
3+
https://leetcode.com/problems/subarray-product-less-than-k/
4+
5+
Your are given an array of positive integers nums.
6+
7+
Count and print the number of (contiguous) subarrays
8+
where the product of all the elements in the subarray is less than k.
9+
*/
10+
11+
// time: 2018年12月27日
12+
13+
package spltk
14+
15+
// sliding window
16+
// time complexity: O(n), where n = len(nums)
17+
// space complexity: O(1)
18+
func numSubArrayProductLessThanK(nums []int, k int) int {
19+
if k <= 0 {
20+
return 0
21+
}
22+
23+
var (
24+
n = len(nums)
25+
l, r int
26+
res int
27+
prod = 1
28+
)
29+
30+
for l < n {
31+
if r < n && prod*nums[r] < k {
32+
prod *= nums[r]
33+
r++
34+
} else if l == r {
35+
l++
36+
r++
37+
} else {
38+
res += r - l
39+
prod /= nums[l]
40+
l++
41+
}
42+
}
43+
return res
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package spltk
2+
3+
import "testing"
4+
5+
func TestNumSubArrayProductLessThanK(t *testing.T) {
6+
testCases := [][]int{
7+
{10, 5, 2, 6},
8+
{10, 5, 2, 6},
9+
{10, 5, 100, 6},
10+
}
11+
12+
ks := []int{100, 0, 100}
13+
expected := []int{8, 0, 4}
14+
for index, data := range testCases {
15+
if res := numSubArrayProductLessThanK(data, ks[index]); res != expected[index] {
16+
t.Errorf("expected %d, got %d", expected[index], res)
17+
}
18+
}
19+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
|0454|[4Sum II](./0454_4sum2/4sum2.go)|Medium||
5555
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
5656
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
57+
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
5758
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
5859
|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /