2762. Continuous Subarrays
Difficulty: Medium
Topics: Sliding Window, Array, Ordered Set, Heap (Priority Queue), Queue, Monotonic Queue, Two Pointers, Ordered Map, Hash Table, Dynamic Programming, Counting, Math, Binary Search Tree, Segment Tree, Tree, Stack, Binary Search, Monotonic Stack, Memoization, Iterator, Greedy, Depth-First Search, Recursion
You are given a 0-indexed integer array nums. A subarray of nums is called continuous if:
Return the total number of continuous subarrays.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Constraints:
Hint:
Solution:
We can use the sliding window technique to efficiently calculate the number of continuous subarrays. We'll maintain a valid window where the difference between the maximum and minimum values in the subarray is at most 2. To efficiently track the maximum and minimum values within the current window, we can use two deques (one for the maximum and one for the minimum).
Let's implement this solution in PHP: 2762. Continuous Subarrays
<?php /** * @param Integer[] $nums * @return Integer */ function continuousSubarrays($nums) { ... ... ... /** * go to ./solution.php */ } // Example usage $nums1 = [5, 4, 2, 4]; echo continuousSubarrays($nums1) . "\n"; // Output: 8 $nums2 = [1, 2, 3]; echo continuousSubarrays($nums2) . "\n"; // Output: 6 ?> <h3> Explanation: </h3> <ol> <li><p><strong>Sliding Window:</strong><br><br> The left pointer moves forward only when the window becomes invalid (i.e., the difference between the maximum and minimum values exceeds 2). The right pointer expands the window by iterating through the array.</p></li> <li> <p><strong>Deques for Maximum and Minimum:</strong></p> <ul> <li>The maxDeque always holds indices of elements in descending order, ensuring the maximum value in the current window is at the front (maxDeque[0]).</li> <li>The minDeque always holds indices of elements in ascending order, ensuring the minimum value in the current window is at the front (minDeque[0]).</li> </ul> </li> <li><p><strong>Counting Subarrays:</strong><br><br> For each right, the number of valid subarrays ending at right is equal to (right - left 1), as all subarrays from left to right are valid.</p></li> <li><p><strong>Efficiency:</strong><br><br> Each element is added to and removed from the deques at most once, making the time complexity <strong>O(n)</strong>. Space complexity is <strong>O(n)</strong> due to the deques.</p></li> </ol> <hr> <h3> Output </h3> <pre class="brush:php;toolbar:false">8 6
Time Complexity:
Space Complexity:
This implementation is efficient and works within the constraints provided.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Continuous Subarrays. For more information, please follow other related articles on the PHP Chinese website!