Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int numSubarrayBoundedMax(int[] nums, int left, int right) {
- return atMost(nums, right) - atMost(nums, left - 1);
- }
- // count number of subarrays with max element less than or equal to "upper"
- public int atMost(int[] nums, int upper) {
- int ans = 0;
- int left = 0;
- for (int right = 0; right < nums.length; right++) {
- if (nums[right] > upper) {
- left = right + 1;
- }
- ans += right - left + 1;
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement