Advertisement
Guest User

Grokking #207

a guest
Feb 9th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. class Solution {
  2.   public int numSubarrayBoundedMax(int[] nums, int left, int right) {
  3.     return atMost(nums, right) - atMost(nums, left - 1);
  4.   }
  5.  
  6.   // count number of subarrays with max element less than or equal to "upper"
  7.   public int atMost(int[] nums, int upper) {
  8.     int ans = 0;
  9.        
  10.     int left = 0;
  11.     for (int right = 0; right < nums.length; right++) {
  12.       if (nums[right] > upper) {
  13.         left = right + 1;
  14.       }
  15.      
  16.       ans += right - left + 1;
  17.     }
  18.    
  19.     return ans;
  20.   }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement