Skip to content

Commit

Permalink
feat(lc): 每日一题
Browse files Browse the repository at this point in the history
  • Loading branch information
changshou83 committed Nov 24, 2022
1 parent 17c9e49 commit 242bd0d
Showing 1 changed file with 36 additions and 0 deletions.
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;
}

0 comments on commit 242bd0d

Please sign in to comment.