LeetCode 3197: Find the Minimum Area to Cover All Ones II Solution

Master LeetCode problem 3197 (Find the Minimum Area to Cover All Ones II), 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.

3197. Find the Minimum Area to Cover All Ones II

Problem Explanation

Explanation:

To solve this problem, we can use dynamic programming. We will iterate over the grid and keep track of the cumulative sum of ones in each row and column. Then, we will iterate over all possible combinations of three non-overlapping rectangles and calculate the minimum sum of their areas that cover all ones.

Steps:

  1. Create prefix sums for rows and columns.
  2. Iterate over all possible combinations of three non-overlapping rectangles to cover all ones.
  3. For each combination, calculate the sum of areas of the rectangles and update the minimum sum.

Time Complexity:

The time complexity of this approach is O(n^2), where n is the size of the grid.

Space Complexity:

The space complexity of this approach is O(n) for storing the prefix sums of rows and columns.

:

Solution Code

class Solution {
    public int minArea(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] rowSum = new int[m][n];
        int[][] colSum = new int[m][n];
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                rowSum[i][j] = (j > 0 ? rowSum[i][j - 1] : 0) + grid[i][j];
                colSum[i][j] = (i > 0 ? colSum[i - 1][j] : 0) + grid[i][j];
            }
        }
        
        int minArea = Integer.MAX_VALUE;
        
        for (int i = 1; i < m - 1; i++) {
            for (int j = i + 1; j < m; j++) {
                int top = rowSum[i - 1][n - 1] - rowSum[i - 1][0];
                int mid = rowSum[j - 1][n - 1] - rowSum[j - 1][0];
                int bottom = rowSum[m - 1][n - 1] - rowSum[j][n - 1];
                
                int topArea = top * (i);
                int midArea = mid * (j - i);
                int bottomArea = bottom * (m - j);
                
                for (int k = 1; k < n; k++) {
                    int left = colSum[m - 1][i - 1] - colSum[0][i - 1];
                    int center = colSum[m - 1][j - 1] - colSum[0][j - 1];
                    int right = colSum[m - 1][n - 1] - colSum[0][n - 1];
                    
                    int leftArea = left * k;
                    int centerArea = center * (n - k);
                    int rightArea = right * (n - k);
                    
                    minArea = Math.min(minArea, topArea + midArea + bottomArea + leftArea + centerArea + rightArea);
                }
            }
        }
        
        return minArea;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3197 (Find the Minimum Area to Cover All Ones II)?

This page provides optimized solutions for LeetCode problem 3197 (Find the Minimum Area to Cover All Ones II) 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 3197 (Find the Minimum Area to Cover All Ones II)?

The time complexity for LeetCode 3197 (Find the Minimum Area to Cover All Ones II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3197 on DevExCode?

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

Back to LeetCode Solutions