File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 605
605
792|[ Number of Matching Subsequences] ( ./0792-number-of-matching-subsequences.js ) |Medium|
606
606
793|[ Preimage Size of Factorial Zeroes Function] ( ./0793-preimage-size-of-factorial-zeroes-function.js ) |Hard|
607
607
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|
608
609
796|[ Rotate String] ( ./0796-rotate-string.js ) |Easy|
609
610
802|[ Find Eventual Safe States] ( ./0802-find-eventual-safe-states.js ) |Medium|
610
611
804|[ Unique Morse Code Words] ( ./0804-unique-morse-code-words.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments