Skip to content

Commit a3ff7e5

Browse files
committed
Add solution #795
1 parent 97c4265 commit a3ff7e5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@
605605
792|[Number of Matching Subsequences](./0792-number-of-matching-subsequences.js)|Medium|
606606
793|[Preimage Size of Factorial Zeroes Function](./0793-preimage-size-of-factorial-zeroes-function.js)|Hard|
607607
794|[Valid Tic-Tac-Toe State](./0794-valid-tic-tac-toe-state.js)|Medium|
608+
795|[Number of Subarrays with Bounded Maximum](./0795-number-of-subarrays-with-bounded-maximum.js)|Medium|
608609
796|[Rotate String](./0796-rotate-string.js)|Easy|
609610
802|[Find Eventual Safe States](./0802-find-eventual-safe-states.js)|Medium|
610611
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 795. Number of Subarrays with Bounded Maximum
3+
* https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums and two integers left and right, return the number of contiguous
7+
* non-empty subarrays such that the value of the maximum array element in that subarray is in
8+
* the range [left, right].
9+
*
10+
* The test cases are generated so that the answer will fit in a 32-bit integer.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @param {number} left
16+
* @param {number} right
17+
* @return {number}
18+
*/
19+
var numSubarrayBoundedMax = function(nums, left, right) {
20+
let result = 0;
21+
let validCount = 0;
22+
let prevInvalidGreater = -1;
23+
24+
for (let i = 0; i < nums.length; i++) {
25+
if (nums[i] > right) {
26+
validCount = 0;
27+
prevInvalidGreater = i;
28+
} else if (nums[i] >= left) {
29+
validCount = i - prevInvalidGreater;
30+
}
31+
32+
result += validCount;
33+
}
34+
35+
return result;
36+
};

0 commit comments

Comments
 (0)