# Find the Minimum Area to Cover All Ones II

11 min readJun 23, 2024

Solve it by different cases

# Problem

This is the 4th question of the LC weekly 304 contest.

3197. Find the Minimum Area to Cover All Ones II

It looks hard, however, if you solve it by different cases, then it is OK.

# Cases:

Since only 3 non overlap rectangles, we only have those 6 cases.

Actually, if we flip the row and col of the input grid, we only need consider about the first 3 cases.

# Critical code

`int ret = INF;`

int this_ret = 0;

int ret_a = 0;

int ret_b = 0;

int ret_c = 0;

int x1, y1, x2, y2;

// case a: 3 rows

if (N>=3){

for (int i = 0; i < N-2; ++i){

x1 = 0, y1 = 0, x2=i, y2 = M -1;

ret_a = get_area(x1, x2, y1, y2);

if (ret_a == 0) continue;

if (ret_a >= ret)continue;

for (int j = i + 1; j < N-1; ++j){

x1 = i+1, y1 = 0, x2=j, y2 = M -1;

ret_b = get_area(x1, x2, y1, y2);

if (ret_b == 0)continue;

this_ret = ret_a + ret_b;

if (this_ret >= ret)continue;

x1 = j+1, y1 = 0, x2=N-1, y2 = M…