You are given a 2D binary array grid. You need to find 3 non-overlapping rectangles having non-zero areas with horizontal and vertical sides such that all the 1's in grid lie inside these rectangles.
Return the minimum possible sum of the area of these rectangles.
Note that the rectangles are allowed to touch.
Example 1:
Input: grid = [[1,0,1],[1,1,1]]
Output: 5
Explanation:

(0, 0) and (1, 0) are covered by a rectangle of area 2.(0, 2) and (1, 2) are covered by a rectangle of area 2.(1, 1) is covered by a rectangle of area 1.Example 2:
Input: grid = [[1,0,1,0],[0,1,0,1]]
Output: 5
Explanation:

(0, 0) and (0, 2) are covered by a rectangle of area 3.(1, 1) is covered by a rectangle of area 1.(1, 3) is covered by a rectangle of area 1.
Constraints:
1 <= grid.length, grid[i].length <= 30grid[i][j] is either 0 or 1.grid.This approach involves identifying all positions of '1' in the grid and using combinatorial methods to select groups of these positions to form the smallest possible set of three non-overlapping rectangles.
The process involves:
This solution uses the brute force method to try every possible way of grouping the ones in the grid into three separate rectangles. The key steps involve iterating over every subset of the available ones, attempting to split them into non-overlapping sets representing the rectangles.
The function calculates areas for each rectangle set if they cover all the 1s while minimizing this value.
Time Complexity: O(2^n * 2^n), where n is the number of '1's, which is feasible due to n ≤ 30.
Space Complexity: O(n) for storing coordinates of the '1's and subsets.
This approach focuses on using dynamic programming, coupled with bitmasking, to efficiently partition the 1s into three separate rectangles with minimal sums.
The implementation details are:
This solution involves the combination of dynamic programming and bitmasking to efficiently compute the minimal area. It calculates rectangular bounds iteratively through the combination of assigning subsets through masks.
Each loop attempts to mask subsets and gather calculations with respect to non-overlapping constraints and minimum areas through partitioning.
Time Complexity: O(3^n), where n is the number of '1's, leveraging bitmasking for dynamic assignment representation.
Space Complexity: O(3^n) due to space needed for storing dynamic state transitions and expressions.
This approach uses dynamic programming to explore different ways of selecting rectangles. We can use a 3D dp array where dp[i][j][k] keeps track of the minimum area to cover the 1's in the grid using up to k rectangles, covering 1's from position 0 to i and 0 to j. The approach involves iterating over possible areas and choosing optimal rectangles.
This C implementation demonstrates the use of dynamic programming to solve the problem....
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^3)
Space Complexity: O(n^3)
In this approach, we use backtracking to explore all possible ways of selecting rectangles. The idea is to backtrack by selecting a starting point for three rectangles and expand each one to cover 1's and then compute the area.
This C solution uses backtracking techniques to find potential rectangles...
C++
Java
Python
C#
JavaScript
Time Complexity: Exponential
Space Complexity: O(n^2)
| Approach | Complexity |
|---|---|
| Brute Force and Combinatorial Splitting | Time Complexity: O(2^n * 2^n), where n is the number of '1's, which is feasible due to n ≤ 30. Space Complexity: O(n) for storing coordinates of the '1's and subsets. |
| Bitmask and Dynamic Programming | Time Complexity: O(3^n), where n is the number of '1's, leveraging bitmasking for dynamic assignment representation. Space Complexity: O(3^n) due to space needed for storing dynamic state transitions and expressions. |
| Dynamic Programming Approach | Time Complexity: O(n^3) Space Complexity: O(n^3) |
| Backtracking Approach | Time Complexity: Exponential Space Complexity: O(n^2) |
8 patterns to solve 80% Leetcode problems • Sahil & Sarra • 656,704 views views
Watch 9 more video solutions →Practice Find the Minimum Area to Cover All Ones II with our built-in code editor and test cases.
Practice on FleetCode