From aa533352020039ccb80d18d3100e01bf7f3bf565 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 9 Dec 2024 23:54:34 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB3152.=20Special=20Array=20II?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/special-array-ii/ Signed-off-by: Matej Focko --- go/special-array-ii.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 go/special-array-ii.go diff --git a/go/special-array-ii.go b/go/special-array-ii.go new file mode 100644 index 0000000..e0f6c4a --- /dev/null +++ b/go/special-array-ii.go @@ -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 +}