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:
Matej Focko 2024-12-09 23:54:34 +01:00
parent a1dc25b5e5
commit aa53335202
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

30
go/special-array-ii.go Normal file
View 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
}