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

713 subarray product less than k solved. #19

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

Merged
zwfang merged 2 commits into master from subarray-product-less-than-k
Dec 28, 2018
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
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ continually updating 😃.
* [350. Intersection of Two Arrays II](./src/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)   *`hash table`*
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`*
* [454. 4Sum II](./src/0454_4sum2/4sum2.go)   *`hash table`*
* [713. Subarray Product Less Than K](src/0713_subarray_product_less_than_k/spltk.go)   *`sliding window`*  *`array`*
* [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)

### String
Expand Down
44 changes: 44 additions & 0 deletions src/0713_subarray_product_less_than_k/spltk.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
713. Subarray Product Less Than K
https://leetcode.com/problems/subarray-product-less-than-k/

Your are given an array of positive integers nums.

Count and print the number of (contiguous) subarrays
where the product of all the elements in the subarray is less than k.
*/

// time: 2018年12月27日

package spltk

// sliding window
// time complexity: O(n), where n = len(nums)
// space complexity: O(1)
func numSubArrayProductLessThanK(nums []int, k int) int {
if k <= 0 {
return 0
}

var (
n = len(nums)
l, r int
res int
prod = 1
)

for l < n {
if r < n && prod*nums[r] < k {
prod *= nums[r]
r++
} else if l == r {
l++
r++
} else {
res += r - l
prod /= nums[l]
l++
}
}
return res
}
19 changes: 19 additions & 0 deletions src/0713_subarray_product_less_than_k/spltk_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package spltk

import "testing"

func TestNumSubArrayProductLessThanK(t *testing.T) {
testCases := [][]int{
{10, 5, 2, 6},
{10, 5, 2, 6},
{10, 5, 100, 6},
}

ks := []int{100, 0, 100}
expected := []int{8, 0, 4}
for index, data := range testCases {
if res := numSubArrayProductLessThanK(data, ks[index]); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@
|0454|[4Sum II](./0454_4sum2/4sum2.go)|Medium||
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
|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||

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