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

grid47
grid47
Exploring patterns and algorithms
Apr 5, 2024 6 min read

You are given a binary array ’nums’. You can divide the array at any index ‘i’ into two parts: ’numsleft’ (before index ‘i’) and ’numsright’ (from index ‘i’ to the end). The division score is the sum of zeros in ’numsleft’ and ones in ’numsright’. Your task is to find all indices where the division score is the highest.
Problem
Approach
Steps
Complexity
Input: The input consists of a binary array 'nums' with length 'n'.
Example: nums = [1, 0, 1, 0]
Constraints:
• 1 <= n <= 10^5
• nums[i] is either 0 or 1
Output: Return a list of indices that give the highest division score.
Example: [3]
Constraints:
• The output should contain all indices where the division score is maximum.
Goal: Iterate through all possible indices, compute the division score, and track the indices that give the highest score.
Steps:
• Count the total number of 1's in the array initially.
• Iterate through each possible division index, calculate the number of 0's in 'numsleft' and the number of 1's in 'numsright'.
• Update the highest division score and collect indices that give the highest score.
Goal: The array will have a length of at least 1 and no more than 10^5 elements, each being either 0 or 1.
Steps:
• 1 <= n <= 10^5
• nums[i] is either 0 or 1
Assumptions:
• The array 'nums' is non-empty and contains only binary values (0 or 1).
Input: Example 1: nums = [1, 0, 1, 0]
Explanation: The division scores at each index are calculated as follows, and the highest score is achieved at indices 3 and 4.

Input: Example 2: nums = [0, 0, 0]
Explanation: The division score is highest at index 3 where all 0's are left in 'numsleft' and none in 'numsright'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus