Skip to content

Commit 5b87638

Browse files
committed
Add solution and test-cases for problem 3152
1 parent dbd4dda commit 5b87638

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

leetcode/3101-3200/3152.Special-Array-II/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [3152.Special Array II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.
5+
6+
You are given an array of integer `nums` and a 2D integer matrix `queries`, where for `queries[i] = [fromi, toi]` your task is to check that
7+
subarray `nums[fromi..toi]` is **special** or not.
8+
9+
Return an array of booleans `answer` such that `answer[i]` is `true` if `nums[fromi..toi]` is special.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
14+
Input: nums = [3,4,1,2,6], queries = [[0,4]]
15+
16+
Output: [false]
1417
15-
## 题意
16-
> ...
18+
Explanation:
1719
18-
## 题解
20+
The subarray is [3,4,1,2,6]. 2 and 6 are both even.
21+
```
22+
23+
**Example 2:**
1924

20-
### 思路1
21-
> ...
22-
Special Array II
23-
```go
2425
```
26+
Input: nums = [4,3,1,6], queries = [[0,2],[2,3]]
27+
28+
Output: [false,true]
2529
30+
Explanation:
31+
32+
1. The subarray is [4,3,1]. 3 and 1 are both odd. So the answer to this query is false.
33+
2. The subarray is [1,6]. There is only one pair: (1,6) and it contains numbers with different parity. So the answer to this query is true.
34+
```
2635

2736
## 结语
2837

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, queries [][]int) []bool {
4+
lq := len(queries)
5+
ans := make([]bool, lq)
6+
l := len(nums)
7+
dp := make([]int, l)
8+
for i := 1; i < l; i++ {
9+
cur := nums[i] & 1
10+
pre := nums[i-1] & 1
11+
if cur != pre {
12+
dp[i] = dp[i-1] + 1
13+
}
14+
}
15+
// 3, 4, 1, 2, 6
16+
// 0, 1, 2, 3, 0
17+
for i, q := range queries {
18+
s, e := q[0], q[1]
19+
if s == e || s >= e-dp[e] {
20+
ans[i] = true
21+
}
22+
}
23+
return ans
524
}

leetcode/3101-3200/3152.Special-Array-II/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums []int
14+
queries [][]int
15+
expect []bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{3, 4, 1, 2, 6}, [][]int{{0, 4}}, []bool{false}},
18+
{"TestCase2", []int{4, 3, 1, 6}, [][]int{{0, 2}, {2, 3}}, []bool{false, true}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)