From 86230669f5cfa089b960e3eda32aa38c18954c80 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Wed, 15 Oct 2025 21:32:38 -0600 Subject: 3350. adjacent increasing subarrays detection 2 --- adjacent_increasing_subarr2.c | 38 ++++++++++++++++++++++++++++++++++++++ adjacent_increasing_subarr2.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 adjacent_increasing_subarr2.c create mode 100644 adjacent_increasing_subarr2.py diff --git a/adjacent_increasing_subarr2.c b/adjacent_increasing_subarr2.c new file mode 100644 index 0000000..3d0c8ec --- /dev/null +++ b/adjacent_increasing_subarr2.c @@ -0,0 +1,38 @@ +// 3350. Adjacent Increasing Subarrays Detection II +#include "leetcode.h" +#include + +/* + * given an array 'nums' of 'n' integers, your task is to find the maximum value + * of 'k' for which there exists two adjacent subarrays of length 'k' each, such + * that both subarrays are strictly increasing. specifically, check if there are + * two subarrays of length 'k' starting at indices 'a' and 'b' where both + * subarrays are strictly increasing, and the subarrays must be adjacent, + * meaning 'b = a + k'. return the maximum possible value of 'k'. a subarrays is + * a contiguous non-empty sequence of elements within an array + */ + +int maxIncreasingSubarrays(int *nums, int numsSize) { + int up = 1, pre = 0, ans = 0; + for (int i = 1; i < numsSize; i++) { + if (nums[i] > nums[i - 1]) + up++; + else { + pre = up; + up = 1; + } + int half = up >> 1; + int m = fmin(pre, up); + int candidate = fmax(half, m); + if (candidate > ans) + ans = candidate; + } + return ans; +} + +int main() { + int n1[] = {2, 5, 7, 8, 9, 2, 3, 4, 3, 1}, + n2[] = {1, 2, 3, 4, 4, 4, 4, 5, 6, 7}; + printf("%d\n", maxIncreasingSubarrays(n1, ARRAY_SIZE(n1))); // expect: 3 + printf("%d\n", maxIncreasingSubarrays(n2, ARRAY_SIZE(n2))); // expect: 2 +} diff --git a/adjacent_increasing_subarr2.py b/adjacent_increasing_subarr2.py new file mode 100644 index 0000000..18d4860 --- /dev/null +++ b/adjacent_increasing_subarr2.py @@ -0,0 +1,39 @@ +# 3350. Adjacent Increasing Subarrays Detection II + +""" +given an array 'nums' of 'n' integers, your task is to find the maximum value +of 'k' for which there exists two adjacent subarrays of length 'k' each, such +that both subarrays are strictly increasing. specifically, check if there are +two subarrays of length 'k' starting at indices 'a' and 'b' where both +subarrays are strictly increasing, and the subarrays must be adjacent, +meaning 'b = a + k'. return the maximum possible value of 'k'. a subarrays is +a contiguous non-empty sequence of elements within an array +""" + + +class Solution(object): + def maxIncreasingSubarrays(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + n, ans = len(nums), 0 + up, pre = 1, 0 + for i in range(1, n): + if nums[i] > nums[i - 1]: + up += 1 + else: + pre = up + up = 1 + half = up >> 1 + m = min(pre, up) + candidate = max(half, m) + if candidate > ans: + ans = candidate + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxIncreasingSubarrays(nums=[2, 5, 7, 8, 9, 2, 3, 4, 3, 1])) + print(obj.maxIncreasingSubarrays(nums=[1, 2, 3, 4, 4, 4, 4, 5, 6, 7])) -- cgit v1.2.3