LeetCode 2155: All Divisions With the Highest Score of a Binary Array Solution

Master LeetCode problem 2155 (All Divisions With the Highest Score of a Binary Array), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

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

Medium

Problem Explanation

Explanation

To solve this problem, we need to iterate through each index of the binary array and calculate the division score at that index. The division score at an index i is the sum of the number of zeros to the left of index i and the number of ones to the right of index i. We will keep track of the maximum division score encountered so far and all the indices that have this maximum score.

  1. Initialize variables maxScore to store the maximum division score, and result to store indices with the maximum score.
  2. Iterate through each index i from 0 to n.
  3. Calculate the division score at index i.
  4. If the score at index i is greater than maxScore, update maxScore and clear result to only include the current index i.
  5. If the score at index i is equal to maxScore, add i to the list of indices in result.
  6. Return the list of indices with the highest division score.

Time Complexity: O(n) where n is the length of the input array. Space Complexity: O(1) since we are only using constant extra space.

Solution Code

class Solution {
    public List<Integer> maxScore(int[] nums) {
        int n = nums.length;
        int maxScore = Integer.MIN_VALUE;
        List<Integer> result = new ArrayList<>();
        
        for (int i = 0; i <= n; i++) {
            int score = countZeros(nums, 0, i - 1) + countOnes(nums, i, n - 1);
            if (score > maxScore) {
                maxScore = score;
                result.clear();
                result.add(i);
            } else if (score == maxScore) {
                result.add(i);
            }
        }
        
        return result;
    }
    
    private int countZeros(int[] nums, int start, int end) {
        int count = 0;
        for (int i = start; i <= end; i++) {
            if (nums[i] == 0) {
                count++;
            }
        }
        return count;
    }
    
    private int countOnes(int[] nums, int start, int end) {
        int count = 0;
        for (int i = start; i <= end; i++) {
            if (nums[i] == 1) {
                count++;
            }
        }
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2155 (All Divisions With the Highest Score of a Binary Array)?

This page provides optimized solutions for LeetCode problem 2155 (All Divisions With the Highest Score of a Binary Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2155 (All Divisions With the Highest Score of a Binary Array)?

The time complexity for LeetCode 2155 (All Divisions With the Highest Score of a Binary Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2155 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2155 in Java, C++, or Python.

Back to LeetCode Solutions