aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--special_array2.c48
-rw-r--r--special_array2.py37
2 files changed, 85 insertions, 0 deletions
diff --git a/special_array2.c b/special_array2.c
new file mode 100644
index 0000000..b929212
--- /dev/null
+++ b/special_array2.c
@@ -0,0 +1,48 @@
+// 3152. Special Array II
+#include "leetcode.h"
+
+/*
+ * 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 array 'matrix' 'queries', where for 'queries[i] =
+ * [from_i, to_i]' your task is to check that subarray 'nums[from_i .. to_i]' is
+ * special or not. return array of booleans 'answer' such that 'answer[i]' is
+ * true if 'nums[from_i .. to_i]' is special
+ */
+
+bool *isArraySpecial(int *nums, int numsSize, int **queries, int queriesSize,
+ int *queriesColSize, int *returnSize) {
+ *returnSize = queriesSize;
+ bool *ans = (bool *)malloc(queriesSize * sizeof(bool));
+ int *bad = (int *)malloc(numsSize * sizeof(int));
+ bad[0] = 0;
+ for (int i = 1; i < numsSize; i++)
+ bad[i] = bad[i - 1] + ((nums[i] % 2 == nums[i - 1] % 2) ? 1 : 0);
+ for (int i = 0; i < queriesSize; i++) {
+ int from = queries[i][0], to = queries[i][1], bad_cnt;
+ bad_cnt = from == to ? 0 : bad[to] - bad[from];
+ ans[i] = !bad_cnt;
+ }
+ free(bad);
+ return ans;
+}
+
+int main() {
+ int n1[] = {3, 4, 1, 2, 6}, q1i[1][2] = {{0, 4}}, rs1;
+ int n2[] = {4, 3, 1, 6}, q2i[2][2] = {{0, 2}, {2, 3}}, rs2;
+ struct two_d_arr q1, q2;
+ two_d_arr_init(&q1, 1, 2, q1i);
+ two_d_arr_init(&q2, 2, 2, q2i);
+ bool *ias1 = isArraySpecial(n1, ARRAY_SIZE(n1), q1.arr, q1.row_size,
+ q1.col_size, &rs1);
+ bool *ias2 = isArraySpecial(n2, ARRAY_SIZE(n2), q2.arr, q2.row_size,
+ q2.col_size, &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", ias1[i]); // expect: 0
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", ias2[i]); // expect: 0 1
+ printf("\n");
+ free(ias1), free(ias2);
+ two_d_arr_free(&q1), two_d_arr_free(&q2);
+}
diff --git a/special_array2.py b/special_array2.py
new file mode 100644
index 0000000..623a302
--- /dev/null
+++ b/special_array2.py
@@ -0,0 +1,37 @@
+# 3152. Special Array II
+
+"""
+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 array 'matrix' 'queries', where for 'queries[i] =
+[from_i, to_i]' your task is to check that subarray 'nums[from_i .. to_i]' is
+special or not. return array of booleans 'answer' such that 'answer[i]' is
+true if 'nums[from_i .. to_i]' is special
+"""
+
+
+class Solution(object):
+ def isArraySpecial(self, nums, queries):
+ """
+ :type nums: List[int]
+ :type queries: List[List[int]]
+ :rtype: List[bool]
+ """
+ pre, ans = [1], []
+ for i in range(1, len(nums)):
+ if nums[i] % 2 == nums[i - 1] % 2:
+ pre.append(pre[-1])
+ else:
+ pre.append(pre[-1] + 1)
+ for s, e in queries:
+ if pre[e] - pre[s] == e - s:
+ ans.append(True)
+ else:
+ ans.append(False)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.isArraySpecial(nums=[3, 4, 1, 2, 6], queries=[[0, 4]]))
+ print(obj.isArraySpecial(nums=[4, 3, 1, 6], queries=[[0, 2], [2, 3]]))