go: add «3152. Special Array II»
URL: https://leetcode.com/problems/special-array-ii/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a1dc25b5e5
commit
aa53335202
1 changed files with 30 additions and 0 deletions
30
go/special-array-ii.go
Normal file
30
go/special-array-ii.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func isArraySpecial(nums []int, queries [][]int) []bool {
|
||||||
|
getMaxReach := func() []int {
|
||||||
|
reach := make([]int, len(nums))
|
||||||
|
|
||||||
|
end := 0
|
||||||
|
for start := 0; start < len(nums); start++ {
|
||||||
|
end = max(end, start)
|
||||||
|
|
||||||
|
for end < len(nums)-1 && nums[end]%2 != nums[end+1]%2 {
|
||||||
|
end++
|
||||||
|
}
|
||||||
|
|
||||||
|
reach[start] = end
|
||||||
|
}
|
||||||
|
|
||||||
|
return reach
|
||||||
|
}
|
||||||
|
maxReach := getMaxReach()
|
||||||
|
|
||||||
|
ans := make([]bool, len(queries))
|
||||||
|
for i, q := range queries {
|
||||||
|
start, end := q[0], q[1]
|
||||||
|
|
||||||
|
ans[i] = end <= maxReach[start]
|
||||||
|
}
|
||||||
|
|
||||||
|
return ans
|
||||||
|
}
|
Loading…
Reference in a new issue