Advertisement

Number of Subarrays with Bounded Maximum - LeetCode 795 Solution

Number of Subarrays with Bounded Maximum - Complete Solution Guide

Number of Subarrays with Bounded Maximum is LeetCode problem 795, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums and two integers left and right , return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range [left, right] . The test cases are generated so that the answer will fit in a 32-bit integer. Example 1: Input: nums = [2,1,4,3], left = 2, right = 3 Output: 3 Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3]. Example 2: Input: nums = [2,9,2,5,6], left = 2, right = 8 O

Detailed Explanation

The problem asks us to find the number of contiguous subarrays within a given integer array `nums` such that the maximum value within each subarray falls within the inclusive range `[left, right]`. We need to return the count of such subarrays. The constraints ensure the answer will fit within a 32-bit integer.

Solution Approach

The solution calculates the number of subarrays with a maximum value less than or equal to `right` and subtracts the number of subarrays with a maximum value less than `left`. This is done by implementing a helper function `count(bound)` which iterates through the array and counts the number of subarrays where the maximum element is less than or equal to `bound`. The function maintains a running count `cur` of the length of the current valid subarray. If an element `x` is less than or equal to `bound`, `cur` is incremented. Otherwise, `cur` is reset to 0. In each iteration, `cur` is added to the total count `ans`.

Step-by-Step Algorithm

  1. Step 1: Define a helper function `count(bound)` that takes an integer `bound` as input.
  2. Step 2: Initialize `ans` (the total number of subarrays) and `cur` (the length of the current valid subarray) to 0.
  3. Step 3: Iterate through the input array `nums`.
  4. Step 4: For each element `x` in `nums`, check if `x` is less than or equal to `bound`.
  5. Step 5: If `x <= bound`, increment `cur` by 1, indicating that the current valid subarray can be extended.
  6. Step 6: If `x > bound`, reset `cur` to 0, because the current valid subarray has ended.
  7. Step 7: Add the value of `cur` to `ans`. This effectively counts all subarrays ending at the current position with maximum value less than or equal to `bound`.
  8. Step 8: After iterating through the entire array, return `ans`.
  9. Step 9: In the main function `numSubarrayBoundedMax`, call `count(right)` to calculate the number of subarrays with a maximum value no greater than `right`.
  10. Step 10: Call `count(left - 1)` to calculate the number of subarrays with a maximum value no greater than `left - 1`.
  11. Step 11: Return the difference between `count(right)` and `count(left - 1)`. This difference represents the number of subarrays whose maximum value falls within the range `[left, right]`.

Key Insights

  • Insight 1: The core idea is to reframe the problem. Instead of directly counting valid subarrays, we can count the total number of subarrays with a maximum value no greater than `right` and subtract the number of subarrays with a maximum value strictly less than `left` (i.e., no greater than `left - 1`).
  • Insight 2: The problem can be solved using a sliding window approach implicitly. We iterate through the array, tracking the length of the current valid subarray (where all elements are within the bound). Whenever we encounter an element outside the bound, we reset the length of the current valid subarray to 0.
  • Insight 3: The function `count(bound)` calculates the number of subarrays with a maximum value less than or equal to `bound` efficiently in O(n) time. Subtracting `count(left - 1)` from `count(right)` gives the number of subarrays whose maximum value falls within the range `[left, right]`.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers.

Companies

Asked at: Quora.