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 933e363

Browse files
feat: add solutions to lc problem: No.3034 (doocs#2369)
No.3034.Number of Subarrays That Match a Pattern I
1 parent b019f45 commit 933e363

File tree

8 files changed

+266
-234
lines changed

8 files changed

+266
-234
lines changed

‎solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md‎

Lines changed: 90 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -53,52 +53,47 @@
5353

5454
## 解法
5555

56-
### 方法一
56+
### 方法一:枚举
57+
58+
我们可以枚举数组 `nums` 的所有长度为 $m + 1$ 的子数组,然后判断是否满足模式数组 `pattern`,如果满足则答案加一。
59+
60+
时间复杂度 $O(n \times m),ドル其中 $n$ 和 $m$ 分别是数组 `nums``pattern` 的长度。空间复杂度 $O(1)$。
5761

5862
<!-- tabs:start -->
5963

6064
```python
6165
class Solution:
6266
def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int:
63-
n = len(nums)
64-
m = len(pattern)
65-
count = 0
66-
for i in range(n - m):
67-
flag = True
68-
for j in range(m):
69-
if (
70-
(pattern[j] == 1 and nums[i + j + 1] <= nums[i + j])
71-
or (pattern[j] == 0 and nums[i + j + 1] != nums[i + j])
72-
or (pattern[j] == -1 and nums[i + j + 1] >= nums[i + j])
73-
):
74-
flag = False
75-
break
76-
if flag:
77-
count += 1
78-
return count
67+
def f(a: int, b: int) -> int:
68+
return 0 if a == b else (1 if a < b else -1)
69+
70+
ans = 0
71+
for i in range(len(nums) - len(pattern)):
72+
ans += all(
73+
f(nums[i + k], nums[i + k + 1]) == p for k, p in enumerate(pattern)
74+
)
75+
return ans
7976
```
8077

8178
```java
8279
class Solution {
8380
public int countMatchingSubarrays(int[] nums, int[] pattern) {
84-
int n = nums.length;
85-
int m = pattern.length;
86-
int count = 0;
87-
for (int i = 0; i <= n - m - 1; i++) {
88-
boolean flag = true;
89-
for (int j = 0; j < m; j++) {
90-
if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) ||
91-
(pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) ||
92-
(pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) {
93-
flag = false;
94-
break;
81+
int n = nums.length, m = pattern.length;
82+
int ans = 0;
83+
for (int i = 0; i < n - m; ++i) {
84+
int ok = 1;
85+
for (int k = 0; k < m && ok == 1; ++k) {
86+
if (f(nums[i + k], nums[i + k + 1]) != pattern[k]) {
87+
ok = 0;
9588
}
9689
}
97-
if (flag) {
98-
count++;
99-
}
90+
ans += ok;
10091
}
101-
return count;
92+
return ans;
93+
}
94+
95+
private int f(int a, int b) {
96+
return a == b ? 0 : (a < b ? 1 : -1);
10297
}
10398
}
10499
```
@@ -107,71 +102,89 @@ class Solution {
107102
class Solution {
108103
public:
109104
int countMatchingSubarrays(vector<int>& nums, vector<int>& pattern) {
110-
int n = nums.size();
111-
int m = pattern.size();
112-
int c = 0;
113-
for (int i = 0; i <= n - m - 1; i++) {
114-
bool flag = true;
115-
for (int j = 0; j < m; j++) {
116-
if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) {
117-
flag = false;
118-
break;
105+
int n = nums.size(), m = pattern.size();
106+
int ans = 0;
107+
auto f = [](int a, int b) {
108+
return a == b ? 0 : (a < b ? 1 : -1);
109+
};
110+
for (int i = 0; i < n - m; ++i) {
111+
int ok = 1;
112+
for (int k = 0; k < m && ok == 1; ++k) {
113+
if (f(nums[i + k], nums[i + k + 1]) != pattern[k]) {
114+
ok = 0;
119115
}
120116
}
121-
if (flag) {
122-
c++;
123-
}
117+
ans += ok;
124118
}
125-
return c;
119+
return ans;
126120
}
127121
};
128122
```
129123
130124
```go
131-
func countMatchingSubarrays(nums []int, pattern []int) int {
132-
n := len(nums)
133-
m := len(pattern)
134-
count := 0
135-
for i := 0; i <= n-m-1; i++ {
136-
flag := true
137-
for j := 0; j < m; j++ {
138-
if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) ||
139-
(pattern[j] == 0 && nums[i+j+1] != nums[i+j]) ||
140-
(pattern[j] == -1 && nums[i+j+1] >= nums[i+j]) {
141-
flag = false
142-
break
143-
}
125+
func countMatchingSubarrays(nums []int, pattern []int) (ans int) {
126+
f := func(a, b int) int {
127+
if a == b {
128+
return 0
144129
}
145-
if flag {
146-
count++
130+
if a < b {
131+
return 1
132+
}
133+
return -1
134+
}
135+
n, m := len(nums), len(pattern)
136+
for i := 0; i < n-m; i++ {
137+
ok := 1
138+
for k := 0; k < m && ok == 1; k++ {
139+
if f(nums[i+k], nums[i+k+1]) != pattern[k] {
140+
ok = 0
141+
}
147142
}
143+
ans += ok
148144
}
149-
return count
145+
return
150146
}
151147
```
152148

153149
```ts
154150
function countMatchingSubarrays(nums: number[], pattern: number[]): number {
155-
const n: number = nums.length;
156-
const m: number = pattern.length;
157-
let count: number = 0;
158-
for (let i = 0; i <= n - m - 1; i++) {
159-
let flag: boolean = true;
160-
for (let j = 0; j < m; j++) {
161-
if (
162-
(pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) ||
163-
(pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) ||
164-
(pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])
165-
) {
166-
flag = false;
167-
break;
151+
const f = (a: number, b: number) => (a === b ? 0 : a < b ? 1 : -1);
152+
const n = nums.length;
153+
const m = pattern.length;
154+
let ans = 0;
155+
for (let i = 0; i < n - m; ++i) {
156+
let ok = 1;
157+
for (let k = 0; k < m && ok; ++k) {
158+
if (f(nums[i + k], nums[i + k + 1]) !== pattern[k]) {
159+
ok = 0;
168160
}
169161
}
170-
if (flag) {
171-
count++;
162+
ans += ok;
163+
}
164+
return ans;
165+
}
166+
```
167+
168+
```cs
169+
public class Solution {
170+
public int CountMatchingSubarrays(int[] nums, int[] pattern) {
171+
int n = nums.Length, m = pattern.Length;
172+
int ans = 0;
173+
for (int i = 0; i < n - m; ++i) {
174+
int ok = 1;
175+
for (int k = 0; k < m && ok == 1; ++k) {
176+
if (f(nums[i + k], nums[i + k + 1]) != pattern[k]) {
177+
ok = 0;
178+
}
179+
}
180+
ans += ok;
172181
}
182+
return ans;
183+
}
184+
185+
private int f(int a, int b) {
186+
return a == b ? 0 : (a < b ? 1 : -1);
173187
}
174-
return count;
175188
}
176189
```
177190

0 commit comments

Comments
(0)

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