LeetCode 1183: Maximum Number of Ones Solution

Master LeetCode problem 1183 (Maximum Number of Ones), a hard 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.

1183. Maximum Number of Ones

Problem Explanation

Explanation:

To solve this problem, we can use a binary search approach on the rows of the given 2D matrix. We can start by counting the number of ones in each row. Then, we can perform a binary search on each row to find the largest subarray with at most k zeros. By iterating through all rows and applying binary search, we can find the maximum number of ones that can be obtained by selecting a subarray with at most k zeros.

Algorithm:

  1. Initialize variables to store the number of rows and columns in the matrix.
  2. Iterate through each row of the matrix and count the number of ones in that row.
  3. Perform a binary search on each row to find the largest subarray with at most k zeros.
  4. Keep track of the maximum number of ones obtained by selecting subarrays from different rows.
  5. Return the maximum number of ones found.

Time Complexity:

The time complexity of this approach is O(m * n * log(n)), where m is the number of rows and n is the number of columns in the matrix.

Space Complexity:

The space complexity of this approach is O(1) as we are not using any extra space apart from a few variables.

: :

Solution Code

class Solution {
    public int maximumOnes(int[][] mat, int k) {
        int rows = mat.length;
        int cols = mat[0].length;
        
        int maxOnes = 0;
        for (int row = 0; row < rows; row++) {
            int ones = 0;
            for (int col = 0; col < cols; col++) {
                if (mat[row][col] == 1) {
                    ones++;
                }
            }
            maxOnes = Math.max(maxOnes, binarySearch(mat[row], k, ones));
        }
        
        return maxOnes;
    }
    
    private int binarySearch(int[] row, int k, int ones) {
        int left = 0, right = 0, maxOnes = 0;
        
        while (right < row.length) {
            if (row[right] == 0) {
                k--;
            }
            while (k < 0) {
                if (row[left] == 0) {
                    k++;
                }
                left++;
            }
            maxOnes = Math.max(maxOnes, right - left + 1);
            right++;
        }
        
        return maxOnes;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1183 (Maximum Number of Ones)?

This page provides optimized solutions for LeetCode problem 1183 (Maximum Number of Ones) 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 1183 (Maximum Number of Ones)?

The time complexity for LeetCode 1183 (Maximum Number of Ones) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1183 on DevExCode?

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

Back to LeetCode Solutions