3152. 特殊数组 II

特殊数组 II

解法一: 预处理

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func isArraySpecial(nums []int, queries [][]int) []bool {
n := len(nums)
dp := make([]int, n)
flag := nums[0] & 1
pre := 0
for i := 1; i < n; i++ {
num := nums[i]
if (num & 1) == flag {
pre = i
}
dp[i] = pre
flag = num & 1
}
ans := make([]bool, len(queries))
for i, query := range queries {
if dp[query[1]] <= query[0] {
ans[i] = true
}
}
return ans
}
作者

wuhunyu

发布于

2024-08-14

更新于

2024-09-05

许可协议