From 6957c22c8b1c8d8855f0d329b8bda900245a15d2 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 27 Mar 2024 09:23:19 +0800 Subject: [PATCH] Add solution and test-cases for problem 713 --- .../README.md | 24 +++++++---------- .../Solution.go | 27 +++++++++++++++++-- .../Solution_test.go | 20 +++++++------- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/leetcode/701-800/0713.Subarray-Product-Less-Than-K/README.md b/leetcode/701-800/0713.Subarray-Product-Less-Than-K/README.md index f057d59d5..ab5d6b7fe 100644 --- a/leetcode/701-800/0713.Subarray-Product-Less-Than-K/README.md +++ b/leetcode/701-800/0713.Subarray-Product-Less-Than-K/README.md @@ -1,28 +1,24 @@ # [713.Subarray Product Less Than K][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 +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [10,5,2,6], k = 100 +Output: 8 +Explanation: The 8 subarrays that have product less than 100 are: +[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6] +Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Subarray Product Less Than K -```go ``` - +Input: nums = [1,2,3], k = 0 +Output: 0 +``` ## 结语 diff --git a/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution.go b/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution.go index d115ccf5e..30fde12f9 100644 --- a/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution.go +++ b/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + cache := make(map[int]int) + ans := 0 + if nums[0] < k { + cache[nums[0]] = 1 + ans++ + } + for i := 1; i < len(nums); i++ { + if nums[i] >= k { + cache = map[int]int{} + continue + } + next := map[int]int{nums[i]: 1} + for kk, v := range cache { + if r := nums[i] * kk; r < k { + next[r] += v + } + } + cache = next + for _, v := range cache { + ans += v + } + } + + return ans } diff --git a/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution_test.go b/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution_test.go index 14ff50eb4..0446e2b40 100644 --- a/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution_test.go +++ b/leetcode/701-800/0713.Subarray-Product-Less-Than-K/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{10, 5, 2, 6}, 100, 8}, + {"TestCase2", []int{1, 2, 3}, 0, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) 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", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }