aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-11-15 21:56:59 -0700
committerJack Sangdahl <[email protected]>2024-11-15 21:56:59 -0700
commit9f64e82838dbdbf3884ffd6ef7ce35fd346e7326 (patch)
tree0b7b11fb8d4025f6169c1df7f9705d38faa8a621
parent6e0dc08f19d42225935112c849d66db2a5ea63c8 (diff)
downloadleetcode-9f64e82838dbdbf3884ffd6ef7ce35fd346e7326.tar.gz
leetcode-9f64e82838dbdbf3884ffd6ef7ce35fd346e7326.zip
3254. find the power of k-size subarrays 1
-rw-r--r--find_power_k_subarr1.c44
-rw-r--r--find_power_k_subarr1.py41
2 files changed, 85 insertions, 0 deletions
diff --git a/find_power_k_subarr1.c b/find_power_k_subarr1.c
new file mode 100644
index 0000000..286b54a
--- /dev/null
+++ b/find_power_k_subarr1.c
@@ -0,0 +1,44 @@
+// 3254. Find the Power of K-Size Subarrays I
+#include "leetcode.h"
+
+/*
+ * you are given an array of integers 'nums' of length 'n' and a positive
+ * integer 'k'. the power of an array is defined as its maximum element if all
+ * its elements are consecutive and sorted in ascending order, -1 otherwise. you
+ * need to find the power of all subarrays of 'nums' of size 'k'. return an
+ * integer array 'results' of size 'n - k + 1' where 'results[i]' is the power
+ * of 'nums[i ... (i + k - 1)]'.
+ */
+
+int *resultsArray(int *nums, int numsSize, int k, int *returnSize) {
+ int *ans = (int *)malloc((numsSize - k + 1) * sizeof(int)), n = 0;
+ for (int i = 0; i <= numsSize - k; i++) {
+ bool consec = true;
+ for (int j = 0; j < k - 1; j++)
+ if (nums[i + j] + 1 != nums[i + j + 1]) {
+ consec = false;
+ break;
+ }
+ ans[n++] = consec ? nums[i + k - 1] : -1;
+ }
+ *returnSize = n;
+ return ans;
+}
+
+int main() {
+ int n1[] = {1, 2, 3, 4, 3, 2, 5}, n2[] = {2, 2, 2, 2, 2},
+ n3[] = {3, 2, 3, 2, 3, 2}, rs1, rs2, rs3;
+ int *ra1 = resultsArray(n1, ARRAY_SIZE(n1), 3, &rs1);
+ int *ra2 = resultsArray(n2, ARRAY_SIZE(n2), 4, &rs2);
+ int *ra3 = resultsArray(n3, ARRAY_SIZE(n3), 2, &rs3);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", ra1[i]); // expect: 3 4 -1 -1 -1
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", ra2[i]); // expect: -1 -1
+ printf("\n");
+ for (int i = 0; i < rs3; i++)
+ printf("%d ", ra3[i]); // expect: -1 3 -1 3 -1
+ printf("\n");
+ free(ra1), free(ra2), free(ra3);
+}
diff --git a/find_power_k_subarr1.py b/find_power_k_subarr1.py
new file mode 100644
index 0000000..b1588ee
--- /dev/null
+++ b/find_power_k_subarr1.py
@@ -0,0 +1,41 @@
+# 3254. Find the Power of K-Size Subarrays I
+from collections import deque
+
+"""
+you are given an array of integers 'nums' of length 'n' and a positive
+integer 'k'. the power of an array is defined as its maximum element if all
+its elements are consecutive and sorted in ascending order, -1 otherwise. you
+need to find the power of all subarrays of 'nums' of size 'k'. return an
+integer array 'results' of size 'n - k + 1' where 'results[i]' is the power
+of 'nums[i ... (i + k - 1)]'.
+"""
+
+
+class Solution(object):
+ def resultsArray(self, nums, k):
+ """
+ :type nums: List[int]
+ :type k: int
+ :rtype: List[int]
+ """
+ if k == 1:
+ return nums
+ n, ans, window = len(nums), [], deque()
+ for i in range(n):
+ while window and i - window[0] >= k:
+ window.popleft()
+ if not window or nums[i] - nums[i - 1] == 1:
+ window.append(i)
+ else:
+ window.clear()
+ window.append(i)
+ if i >= k - 1:
+ ans.append(nums[i] if len(window) == k else -1)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.resultsArray(nums=[1, 2, 3, 4, 3, 2, 5], k=3))
+ print(obj.resultsArray(nums=[2, 2, 2, 2, 2], k=4))
+ print(obj.resultsArray(nums=[3, 2, 3, 2, 3, 2], k=2))