File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 725
725
912|[ Sort an Array] ( ./solutions/0912-sort-an-array.js ) |Medium|
726
726
913|[ Cat and Mouse] ( ./solutions/0913-cat-and-mouse.js ) |Hard|
727
727
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|
728
729
916|[ Word Subsets] ( ./solutions/0916-word-subsets.js ) |Medium|
729
730
918|[ Maximum Sum Circular Subarray] ( ./solutions/0918-maximum-sum-circular-subarray.js ) |Medium|
730
731
922|[ Sort Array By Parity II] ( ./solutions/0922-sort-array-by-parity-ii.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments