Skip to content

Commit 2602592

Browse files
authored
Create 2972-count-the-number-of-incremovable-subarrays-ii.js
1 parent cad1ed6 commit 2602592

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var incremovableSubarrayCount = function(nums) {
6+
let n = nums.length;
7+
let l = 0;
8+
while (nums[l + 1] > nums[l]) ++l;
9+
if (l + 1 == n) return (n * (n + 1)) / 2;
10+
11+
let res = l + 2;
12+
let r = n;
13+
do {
14+
--r;
15+
while (l >= 0 && nums[l] >= nums[r]) --l;
16+
res += l + 2
17+
}
18+
while (nums[r] > nums[r-1])
19+
20+
return res;
21+
};

0 commit comments

Comments
 (0)