Leetcode Problem 2155. All Divisions With the Highest Score of a Binary Array

2155. All Divisions With the Highest Score of a Binary Array

Leetcode Solutions

Prefix Sum and Postfix Sum Approach

  1. Initialize variables to keep track of the count of zeros (leftZero) and ones (rightOne), and a variable to store the maximum score (maxScore).
  2. Calculate the total number of ones in the array to initialize rightOne.
  3. Initialize an empty list to store the indices with the maximum score (maxIndices).
  4. Iterate through the array from index 0 to n (inclusive), where n is the length of the array.
  5. At each index i, calculate the current division score as the sum of leftZero and rightOne.
  6. If the current division score is greater than maxScore, update maxScore and reset maxIndices to only contain the current index i.
  7. If the current division score equals maxScore, append the current index i to maxIndices.
  8. Update leftZero and rightOne based on the value at the current index i.
  9. After the loop, return maxIndices as the final result.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...