Skip to content
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
35 changes: 22 additions & 13 deletions leetcode/3101-3200/3152.Special-Array-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [3152.Special Array II][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
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.

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
subarray `nums[fromi..toi]` is **special** or not.

Return an array of booleans `answer` such that `answer[i]` is `true` if `nums[fromi..toi]` is special.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: nums = [3,4,1,2,6], queries = [[0,4]]

Output: [false]

## 题意
> ...
Explanation:

## 题解
The subarray is [3,4,1,2,6]. 2 and 6 are both even.
```

**Example 2:**

### 思路1
> ...
Special Array II
```go
```
Input: nums = [4,3,1,6], queries = [[0,2],[2,3]]

Output: [false,true]

Explanation:

1. The subarray is [4,3,1]. 3 and 1 are both odd. So the answer to this query is false.
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.
```

## 结语

Expand Down
23 changes: 21 additions & 2 deletions leetcode/3101-3200/3152.Special-Array-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, queries [][]int) []bool {
lq := len(queries)
ans := make([]bool, lq)
l := len(nums)
dp := make([]int, l)
for i := 1; i < l; i++ {
cur := nums[i] & 1
pre := nums[i-1] & 1
if cur != pre {
dp[i] = dp[i-1] + 1
}
}
// 3, 4, 1, 2, 6
// 0, 1, 2, 3, 0
for i, q := range queries {
s, e := q[0], q[1]
if s == e || s >= e-dp[e] {
ans[i] = true
}
}
return ans
}
22 changes: 11 additions & 11 deletions leetcode/3101-3200/3152.Special-Array-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums []int
queries [][]int
expect []bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{3, 4, 1, 2, 6}, [][]int{{0, 4}}, []bool{false}},
{"TestCase2", []int{4, 3, 1, 6}, [][]int{{0, 2}, {2, 3}}, []bool{false, true}},
}

// 开始测试
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.queries)
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.queries)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading