File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number[] } changeIndices
4+ * @return {number }
5+ */
6+ var earliestSecondToMarkIndices = function ( nums , changeIndices ) {
7+ let isPossible = function ( mid ) {
8+ let last = new Uint16Array ( nums . length ) ;
9+ for ( let i = 0 ; i <= mid ; ++ i ) last [ changeIndices [ i ] ] = i ;
10+
11+ let count = 0 , marked = 0 ;
12+ for ( let i = 0 ; i <= mid ; ++ i ) {
13+ if ( last [ changeIndices [ i ] ] === i ) {
14+ count -= nums [ changeIndices [ i ] ] ;
15+ if ( count < 0 ) return false ;
16+ ++ marked ;
17+ } else {
18+ ++ count ;
19+ }
20+ }
21+ return marked === nums . length ;
22+ } ;
23+
24+ changeIndices = changeIndices . map ( ( x ) => x - 1 ) ;
25+ let l = 0 , r = changeIndices . length - 1 ;
26+ while ( l < r ) {
27+ let mid = ( l + r ) >> 1 ;
28+ isPossible ( mid ) ? r = mid : l = mid + 1 ;
29+ }
30+ return isPossible ( l ) ? l + 1 : - 1 ;
31+ } ;
You can’t perform that action at this time.
0 commit comments