From 7b3cf846426f5ad86dbdc9d1466fedc08530f0c5 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 19 Jul 2025 09:20:47 +0800 Subject: [PATCH] Add solution and test-cases for problem 3034 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 26 +++++++++++++-- .../Solution_test.go | 21 ++++++------ 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/README.md b/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/README.md index be2c4fb2f..16fb4a856 100755 --- a/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/README.md +++ b/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/README.md @@ -1,28 +1,33 @@ # [3034.Number of Subarrays That Match a Pattern I][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 +You are given a **0-indexed** integer array `nums` of size `n`, and a **0-indexed** integer array `pattern` of size `m` consisting of integers `-1`, `0`, and `1`. + +A `subarray nums[i..j]` of size `m + 1` is said to match the `pattern` if the following conditions hold for each element `pattern[k]`: + +- `nums[i + k + 1] > nums[i + k] if pattern[k] == 1`. +- `nums[i + k + 1] == nums[i + k] if pattern[k] == 0`. +- `nums[i + k + 1] < nums[i + k] if pattern[k] == -1`. + +Return the **count** of subarrays in nums that match the `pattern`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,3,4,5,6], pattern = [1,1] +Output: 4 +Explanation: The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern. +Hence, there are 4 subarrays in nums that match the pattern. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Number of Subarrays That Match a Pattern I -```go ``` - +Input: nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1] +Output: 2 +Explanation: Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern. +Hence, there are 2 subarrays in nums that match the pattern. +``` ## 结语 diff --git a/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution.go b/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution.go index d115ccf5e..da2373c2d 100644 --- a/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution.go +++ b/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution.go @@ -1,5 +1,27 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, pattern []int) int { + var ans int + m := len(pattern) + n := len(nums) + k := 0 + for i := 0; i < n-m; i++ { + k = 0 + for ; k < m; k++ { + if pattern[k] == 1 && nums[i+k+1] > nums[i+k] { + continue + } + if pattern[k] == 0 && nums[i+k+1] == nums[i+k] { + continue + } + if pattern[k] == -1 && nums[i+k+1] < nums[i+k] { + continue + } + break + } + if k == m { + ans++ + } + } + return ans } diff --git a/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution_test.go b/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution_test.go index 14ff50eb4..fac219f09 100644 --- a/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution_test.go +++ b/leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + inputs, pattern []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3, 4, 5, 6}, []int{1, 1}, 4}, + {"TestCase2", []int{1, 4, 4, 1, 3, 5, 5, 3}, []int{1, 0, -1}, 2}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.pattern) 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.inputs, c.pattern) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }