Skip to content

Commit 0f13855

Browse files
committed
Add solution #915
1 parent 0c0f57b commit 0f13855

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@
725725
912|[Sort an Array](./solutions/0912-sort-an-array.js)|Medium|
726726
913|[Cat and Mouse](./solutions/0913-cat-and-mouse.js)|Hard|
727727
914|[X of a Kind in a Deck of Cards](./solutions/0914-x-of-a-kind-in-a-deck-of-cards.js)|Medium|
728+
915|[Partition Array into Disjoint Intervals](./solutions/0915-partition-array-into-disjoint-intervals.js)|Medium|
728729
916|[Word Subsets](./solutions/0916-word-subsets.js)|Medium|
729730
918|[Maximum Sum Circular Subarray](./solutions/0918-maximum-sum-circular-subarray.js)|Medium|
730731
922|[Sort Array By Parity II](./solutions/0922-sort-array-by-parity-ii.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 915. Partition Array into Disjoint Intervals
3+
* https://leetcode.com/problems/partition-array-into-disjoint-intervals/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, partition it into two (contiguous) subarrays left and right so that:
7+
* - Every element in left is less than or equal to every element in right.
8+
* - left and right are non-empty.
9+
* - left has the smallest possible size.
10+
*
11+
* Return the length of left after such a partitioning.
12+
*
13+
* Test cases are generated such that partitioning exists.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var partitionDisjoint = function(nums) {
21+
let leftMax = nums[0];
22+
let currentMax = nums[0];
23+
let partitionIndex = 0;
24+
25+
for (let i = 1; i < nums.length; i++) {
26+
if (nums[i] < leftMax) {
27+
partitionIndex = i;
28+
leftMax = currentMax;
29+
}
30+
currentMax = Math.max(currentMax, nums[i]);
31+
}
32+
33+
return partitionIndex + 1;
34+
};

0 commit comments

Comments
 (0)