Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8ab9d01

Browse files
committed
Add solution #2155
1 parent 6845bfe commit 8ab9d01

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,780 LeetCode solutions in JavaScript
1+
# 1,781 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1650,6 +1650,7 @@
16501650
2150|[Find All Lonely Numbers in the Array](./solutions/2150-find-all-lonely-numbers-in-the-array.js)|Medium|
16511651
2151|[Maximum Good People Based on Statements](./solutions/2151-maximum-good-people-based-on-statements.js)|Hard|
16521652
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|
16531654
2161|[Partition Array According to Given Pivot](./solutions/2161-partition-array-according-to-given-pivot.js)|Medium|
16541655
2176|[Count Equal and Divisible Pairs in an Array](./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js)|Easy|
16551656
2179|[Count Good Triplets in an Array](./solutions/2179-count-good-triplets-in-an-array.js)|Hard|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
};

0 commit comments

Comments
 (0)