Home > Backend Development > PHP Tutorial > Continuous Subarrays

Continuous Subarrays

Mary-Kate Olsen
Release: 2024-12-25 05:46:15
Original
831 people have browsed it

Continuous Subarrays

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:

  • Let i, i 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2.

Return the total number of continuous subarrays.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

  • Input: nums = [5,4,2,4]
  • Output: 8
  • Explanation:
    • Continuous subarray of size 1: [5], [4], [2], [4].
    • Continuous subarray of size 2: [5,4], [4,2], [2,4].
    • Continuous subarray of size 3: [4,2,4].
    • There are no subarrys of size 4.
    • Total continuous subarrays = 4 3 1 = 8.
    • It can be shown that there are no more continuous subarrays.

Example 2:

  • Input: nums = [1,2,3]
  • Output: 6
  • Explanation:
    • Continuous subarray of size 1: [1], [2], [3].
    • Continuous subarray of size 2: [1,2], [2,3].
    • Continuous subarray of size 3: [1,2,3].
    • Total continuous subarrays = 3 2 1 = 6.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Hint:

  1. Try using the sliding window technique.
  2. Use a set or map to keep track of the maximum and minimum of subarrays.

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).

Approach

  1. Use the sliding window technique with two pointers: left and right.
  2. Use two deques:
    • One to track indices of elements in descending order for the maximum value.
    • One to track indices of elements in ascending order for the minimum value.
  3. For each index right:
    • Update the deques to reflect the current window.
    • Ensure the window remains valid (difference between maximum and minimum ≤ 2). If invalid, increment the left pointer to shrink the window.
    • Count the number of valid subarrays ending at index right as (right - left 1).
  4. Return the total count of subarrays.

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
Copy after login

Complexity Analysis

  1. Time Complexity:

    • Each element is pushed and popped from the deques at most once.
    • Total time complexity is O(n).
  2. Space Complexity:

    • Deques store indices, with a maximum size of n.
    • Space complexity is O(n).

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:

  • LinkedIn
  • GitHub

The above is the detailed content of Continuous Subarrays. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template