-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17c9e49
commit 242bd0d
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 将数组中的数分为三类,0:小于left,1:大于等于left,小于等于right,2:大于right | ||
// 双指针:计算出1,2的数量减去2 | ||
function numsSubarrayBoundedMax(nums, l, r) { | ||
let res = 0, | ||
last1 = 0, | ||
last2 = 0; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] >= left && nums[i] <= right) { | ||
last1 = i; | ||
} else if (nums[i] > right) { | ||
last2 = i; | ||
last1 = -1; | ||
} | ||
|
||
if (last1 !== -1) { | ||
res += last1 - last2; | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
// 计数:计算出0,1的数量减去0 | ||
function numsSubarrayBoundedMax(nums, l, r) { | ||
return count(nums, right) - count(nums, left - 1); | ||
} | ||
|
||
// 返回最大值小于等于lower的子数组数目 | ||
function count(nums, lower) { | ||
let res = 0, | ||
cur = 0; | ||
for (const x of nums) { | ||
cur = x < lower ? cur + 1 : 0; | ||
res += cur; | ||
} | ||
return res; | ||
} |