From 5b876388879db282eab89f14c0374da5489d4014 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 9 Dec 2024 09:27:37 +0800 Subject: [PATCH] Add solution and test-cases for problem 3152 --- .../3101-3200/3152.Special-Array-II/README.md | 35 ++++++++++++------- .../3152.Special-Array-II/Solution.go | 23 ++++++++++-- .../3152.Special-Array-II/Solution_test.go | 22 ++++++------ 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/leetcode/3101-3200/3152.Special-Array-II/README.md b/leetcode/3101-3200/3152.Special-Array-II/README.md index 4a3a744e7..6bb46a50c 100755 --- a/leetcode/3101-3200/3152.Special-Array-II/README.md +++ b/leetcode/3101-3200/3152.Special-Array-II/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3101-3200/3152.Special-Array-II/Solution.go b/leetcode/3101-3200/3152.Special-Array-II/Solution.go index d115ccf5e..462dbd437 100644 --- a/leetcode/3101-3200/3152.Special-Array-II/Solution.go +++ b/leetcode/3101-3200/3152.Special-Array-II/Solution.go @@ -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 } diff --git a/leetcode/3101-3200/3152.Special-Array-II/Solution_test.go b/leetcode/3101-3200/3152.Special-Array-II/Solution_test.go index 14ff50eb4..a5a9e6b6b 100644 --- a/leetcode/3101-3200/3152.Special-Array-II/Solution_test.go +++ b/leetcode/3101-3200/3152.Special-Array-II/Solution_test.go @@ -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() { }