File tree 2 files changed +47
-1
lines changed 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,780 LeetCode solutions in JavaScript
1
+ # 1,781 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1650
1650
2150|[ Find All Lonely Numbers in the Array] ( ./solutions/2150-find-all-lonely-numbers-in-the-array.js ) |Medium|
1651
1651
2151|[ Maximum Good People Based on Statements] ( ./solutions/2151-maximum-good-people-based-on-statements.js ) |Hard|
1652
1652
2154|[ Keep Multiplying Found Values by Two] ( ./solutions/2154-keep-multiplying-found-values-by-two.js ) |Easy|
1653
+ 2155|[ All Divisions With the Highest Score of a Binary Array] ( ./solutions/2155-all-divisions-with-the-highest-score-of-a-binary-array.js ) |Medium|
1653
1654
2161|[ Partition Array According to Given Pivot] ( ./solutions/2161-partition-array-according-to-given-pivot.js ) |Medium|
1654
1655
2176|[ Count Equal and Divisible Pairs in an Array] ( ./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js ) |Easy|
1655
1656
2179|[ Count Good Triplets in an Array] ( ./solutions/2179-count-good-triplets-in-an-array.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2155. All Divisions With the Highest Score of a Binary Array
3
+ * https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a 0-indexed binary array nums of length n. nums can be divided at index i
7
+ * (where 0 <= i <= n) into two arrays (possibly empty) numsleft and numsright:
8
+ * - numsleft has all the elements of nums between index 0 and i - 1 (inclusive), while numsright
9
+ * has all the elements of nums between index i and n - 1 (inclusive).
10
+ * - If i == 0, numsleft is empty, while numsright has all the elements of nums.
11
+ * - If i == n, numsleft has all the elements of nums, while numsright is empty.
12
+ *
13
+ * The division score of an index i is the sum of the number of 0's in numsleft and the number
14
+ * of 1's in numsright.
15
+ *
16
+ * Return all distinct indices that have the highest possible division score. You may return
17
+ * the answer in any order.
18
+ */
19
+
20
+ /**
21
+ * @param {number[] } nums
22
+ * @return {number[] }
23
+ */
24
+ var maxScoreIndices = function ( nums ) {
25
+ const result = [ 0 ] ;
26
+ let leftZeros = 0 ;
27
+ let rightOnes = nums . reduce ( ( sum , num ) => sum + num , 0 ) ;
28
+ let maxScore = rightOnes ;
29
+
30
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
31
+ leftZeros += nums [ i ] === 0 ? 1 : 0 ;
32
+ rightOnes -= nums [ i ] ;
33
+ const score = leftZeros + rightOnes ;
34
+
35
+ if ( score > maxScore ) {
36
+ maxScore = score ;
37
+ result . length = 0 ;
38
+ result . push ( i + 1 ) ;
39
+ } else if ( score === maxScore ) {
40
+ result . push ( i + 1 ) ;
41
+ }
42
+ }
43
+
44
+ return result ;
45
+ } ;
You can’t perform that action at this time.
0 commit comments