Skip to content

Latest commit

 

History

History
60 lines (49 loc) · 1.69 KB

File metadata and controls

60 lines (49 loc) · 1.69 KB

2444. Count Subarrays With Fixed Bounds

You are given an integer array nums and two integers minK and maxK.

A fixed-bound subarray of nums is a subarray that satisfies the following conditions:

  • The minimum value in the subarray is equal to minK.
  • The maximum value in the subarray is equal to maxK.

Return the number of fixed-bound subarrays.

A subarray is a contiguous part of an array.

Example 1:

Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
Output: 2
Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].

Example 2:

Input: nums = [1,1,1,1], minK = 1, maxK = 1
Output: 10
Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.

Constraints:

  • 2 <= nums.length <= 105
  • 1 <= nums[i], minK, maxK <= 106

Solutions (Rust)

1. Solution

impl Solution {
    pub fn count_subarrays(nums: Vec<i32>, min_k: i32, max_k: i32) -> i64 {
        let mut last_ban = -1;
        let mut last_min = -1;
        let mut last_max = -1;
        let mut ret = 0;

        for i in 0..nums.len() {
            if nums[i] < min_k || nums[i] > max_k {
                last_ban = i as i64;
            }
            if nums[i] == min_k {
                last_min = i as i64;
            }
            if nums[i] == max_k {
                last_max = i as i64;
            }

            if last_min > last_ban && last_max > last_ban {
                ret += last_min.min(last_max) - last_ban;
            }
        }

        ret
    }
}