Skip to content

Commit 23b2af9

Browse files
authored
Create 915-partition-array-into-disjoint-intervals.js
1 parent 9b1f719 commit 23b2af9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number}
4+
*/
5+
const partitionDisjoint = function(A) {
6+
let n = A.length;
7+
let maxLeft = A[0];
8+
let minRight = new Int32Array(n);
9+
let min = Infinity;
10+
for (let i = n - 1; i >= 1; i--) {
11+
min = Math.min(min, A[i]);
12+
minRight[i] = min;
13+
}
14+
15+
for(let i = 1; i < n; i++){
16+
if (maxLeft <= minRight[i]){
17+
return i;
18+
}
19+
20+
maxLeft = Math.max(maxLeft, A[i]);
21+
}
22+
};

0 commit comments

Comments
 (0)