Class Solution

java.lang.Object
g2701_2800.s2762_continuous_subarrays.Solution

public class Solution extends Object
2762 - Continuous Subarrays.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A subarray of <code>nums</code> is called <strong>continuous</strong> if:</p> <ul> <li>Let <code>i</code>, <code>i + 1</code>, &hellip;, <code>j</code> be the indices in the subarray. Then, for each pair of indices <code>i <= i<sub>1</sub>, i<sub>2</sub> <= j</code>, <code>0 <= |nums[i<sub>1</sub>] - nums[i<sub>2</sub>]| <= 2</code>.</li> </ul> <p>Return <em>the total number of <strong>continuous</strong> subarrays.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [5,4,2,4]</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong></p> <p>Continuous subarray of size 1: [5], [4], [2], [4].</p> <p>Continuous subarray of size 2: [5,4], [4,2], [2,4].</p> <p>Continuous subarray of size 3: [4,2,4].</p> <p>Thereare no subarrys of size 4.</p> <p>Total continuous subarrays = 4 + 3 + 1 = 8.</p> <p>It can be shown that there are no more continuous subarrays.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,3]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <p>Continuous subarray of size 1: [1], [2], [3].</p> <p>Continuous subarray of size 2: [1,2], [2,3].</p> <p>Continuous subarray of size 3: [1,2,3].</p> <p>Total continuous subarrays = 3 + 2 + 1 = 6.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • continuousSubarrays

      public long continuousSubarrays(int[] nums)