Skip to content

Commit 60cc6fe

Browse files
authored
Create 3197-find-the-minimum-area-to-cover-all-ones-ii.js
1 parent cca3054 commit 60cc6fe

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
const minimumSum = function(grid) {
6+
const m = grid.length, n = grid[0].length
7+
let res = Infinity
8+
// case 1
9+
/*
10+
1 | 2 | 3
11+
*/
12+
for(let i = 0; i < n - 2; i++) {
13+
const one = calc(0, m - 1, 0, i, grid)
14+
for(let j = i + 1; j < n - 1; j++) {
15+
const two = calc(0, m - 1, i + 1, j, grid)
16+
const three = calc(0, m - 1, j + 1, n - 1, grid)
17+
res = Math.min(res, one + two + three)
18+
}
19+
}
20+
21+
22+
// case 2
23+
/*
24+
1
25+
-
26+
2
27+
-
28+
3
29+
*/
30+
for(let i = 0; i < m - 2; i++) {
31+
const one = calc(0, i, 0, n - 1, grid)
32+
for(let j = i + 1; j < m - 1; j++) {
33+
const two = calc(i + 1, j, 0, n - 1, grid)
34+
const three = calc(j + 1, m - 1, 0, n - 1, grid)
35+
res = Math.min(res, one + two + three)
36+
}
37+
}
38+
39+
40+
// case 3
41+
/*
42+
2 | 3
43+
-----
44+
1
45+
*/
46+
for(let i = m - 1; i >= 1; i--) {
47+
const one = calc(i, m - 1, 0, n - 1, grid)
48+
for(let j = 0; j < n - 1; j++) {
49+
const two = calc(0, i - 1, 0, j, grid)
50+
const three = calc(0, i - 1, j + 1, n - 1, grid)
51+
res = Math.min(res, one + two + three)
52+
}
53+
54+
}
55+
56+
57+
// case 4
58+
/*
59+
2 |
60+
--| 1
61+
3 |
62+
*/
63+
for(let i = n - 1; i >= 1; i--) {
64+
const one = calc(0, m - 1, i, n - 1, grid)
65+
for(let j = 0; j < m - 1; j++) {
66+
const two = calc(0, j, 0, i - 1, grid)
67+
const three = calc(j + 1, m - 1, 0, i - 1, grid)
68+
res = Math.min(res, one + two + three)
69+
}
70+
}
71+
72+
73+
// case 5
74+
/*
75+
1
76+
-----
77+
2 | 3
78+
*/
79+
for(let i = 0; i < m - 1; i++) {
80+
const one = calc(0, i, 0, n - 1, grid)
81+
for(let j = 0; j < n - 1; j++) {
82+
const two = calc(i + 1, m - 1, 0, j, grid)
83+
const three = calc(i + 1, m - 1, j + 1, n - 1, grid)
84+
res = Math.min(res, one + two + three)
85+
}
86+
87+
}
88+
89+
90+
// case 6
91+
/*
92+
| 2
93+
1 |--
94+
| 3
95+
*/
96+
for(let j = 0; j < n - 1; j++) {
97+
const one = calc(0, m - 1, 0, j, grid)
98+
for(let i = 0; i < m - 1; i++) {
99+
const two = calc(0, i, j + 1, n - 1, grid)
100+
const three = calc(i + 1, m - 1, j + 1, n - 1, grid)
101+
res = Math.min(res, one + two + three)
102+
}
103+
}
104+
105+
return res
106+
};
107+
108+
function calc(rs, re, cs, ce, grid) {
109+
110+
let rmin = Infinity, rmax = -Infinity, cmin = Infinity, cmax = -Infinity
111+
for(let i = rs; i <= re; i++) {
112+
for(let j = cs; j <= ce; j++) {
113+
if(grid[i][j] === 1) {
114+
rmin = Math.min(rmin, i)
115+
rmax = Math.max(rmax, i)
116+
cmin = Math.min(cmin, j)
117+
cmax = Math.max(cmax, j)
118+
}
119+
}
120+
}
121+
return (rmax - rmin + 1) * (cmax - cmin + 1)
122+
}

0 commit comments

Comments
 (0)