Skip to content

feat: add solutions to lc problems: No.3195,3197 #4659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,84 @@ function minimumArea(grid: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut x1 = m as i32;
let mut y1 = n as i32;
let mut x2 = 0i32;
let mut y2 = 0i32;

for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 {
x1 = x1.min(i as i32);
y1 = y1.min(j as i32);
x2 = x2.max(i as i32);
y2 = y2.max(j as i32);
}
}
}

(x2 - x1 + 1) * (y2 - y1 + 1)
}
}
```

#### JavaScript

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var minimumArea = function (grid) {
const [m, n] = [grid.length, grid[0].length];
let [x1, y1] = [m, n];
let [x2, y2] = [0, 0];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
x1 = Math.min(x1, i);
y1 = Math.min(y1, j);
x2 = Math.max(x2, i);
y2 = Math.max(y2, j);
}
}
}
return (x2 - x1 + 1) * (y2 - y1 + 1);
};
```

#### C#

```cs
public class Solution {
public int MinimumArea(int[][] grid) {
int m = grid.Length, n = grid[0].Length;
int x1 = m, y1 = n;
int x2 = 0, y2 = 0;

for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
x1 = Math.Min(x1, i);
y1 = Math.Min(y1, j);
x2 = Math.Max(x2, i);
y2 = Math.Max(y2, j);
}
}
}

return (x2 - x1 + 1) * (y2 - y1 + 1);
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,84 @@ function minimumArea(grid: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut x1 = m as i32;
let mut y1 = n as i32;
let mut x2 = 0i32;
let mut y2 = 0i32;

for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 {
x1 = x1.min(i as i32);
y1 = y1.min(j as i32);
x2 = x2.max(i as i32);
y2 = y2.max(j as i32);
}
}
}

(x2 - x1 + 1) * (y2 - y1 + 1)
}
}
```

#### JavaScript

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var minimumArea = function (grid) {
const [m, n] = [grid.length, grid[0].length];
let [x1, y1] = [m, n];
let [x2, y2] = [0, 0];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
x1 = Math.min(x1, i);
y1 = Math.min(y1, j);
x2 = Math.max(x2, i);
y2 = Math.max(y2, j);
}
}
}
return (x2 - x1 + 1) * (y2 - y1 + 1);
};
```

#### C#

```cs
public class Solution {
public int MinimumArea(int[][] grid) {
int m = grid.Length, n = grid[0].Length;
int x1 = m, y1 = n;
int x2 = 0, y2 = 0;

for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
x1 = Math.Min(x1, i);
y1 = Math.Min(y1, j);
x2 = Math.Max(x2, i);
y2 = Math.Max(y2, j);
}
}
}

return (x2 - x1 + 1) * (y2 - y1 + 1);
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Solution {
public int MinimumArea(int[][] grid) {
int m = grid.Length, n = grid[0].Length;
int x1 = m, y1 = n;
int x2 = 0, y2 = 0;

for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
x1 = Math.Min(x1, i);
y1 = Math.Min(y1, j);
x2 = Math.Max(x2, i);
y2 = Math.Max(y2, j);
}
}
}

return (x2 - x1 + 1) * (y2 - y1 + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[][]} grid
* @return {number}
*/
var minimumArea = function (grid) {
const [m, n] = [grid.length, grid[0].length];
let [x1, y1] = [m, n];
let [x2, y2] = [0, 0];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
x1 = Math.min(x1, i);
y1 = Math.min(y1, j);
x2 = Math.max(x2, i);
y2 = Math.max(y2, j);
}
}
}
return (x2 - x1 + 1) * (y2 - y1 + 1);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
impl Solution {
pub fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut x1 = m as i32;
let mut y1 = n as i32;
let mut x2 = 0i32;
let mut y2 = 0i32;

for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 {
x1 = x1.min(i as i32);
y1 = y1.min(j as i32);
x2 = x2.max(i as i32);
y2 = y2.max(j as i32);
}
}
}

(x2 - x1 + 1) * (y2 - y1 + 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,154 @@ function minimumSum(grid: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_sum(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut ans = (m * n) as i32;
let inf = i32::MAX / 4;

let f = |i1: usize, j1: usize, i2: usize, j2: usize| -> i32 {
let mut x1 = inf;
let mut y1 = inf;
let mut x2 = -inf;
let mut y2 = -inf;

for i in i1..=i2 {
for j in j1..=j2 {
if grid[i][j] == 1 {
x1 = x1.min(i as i32);
y1 = y1.min(j as i32);
x2 = x2.max(i as i32);
y2 = y2.max(j as i32);
}
}
}
if x1 > x2 || y1 > y2 {
inf
} else {
(x2 - x1 + 1) * (y2 - y1 + 1)
}
};

for i1 in 0..m - 1 {
for i2 in i1 + 1..m - 1 {
ans = ans.min(
f(0, 0, i1, n - 1)
+ f(i1 + 1, 0, i2, n - 1)
+ f(i2 + 1, 0, m - 1, n - 1),
);
}
}

for j1 in 0..n - 1 {
for j2 in j1 + 1..n - 1 {
ans = ans.min(
f(0, 0, m - 1, j1)
+ f(0, j1 + 1, m - 1, j2)
+ f(0, j2 + 1, m - 1, n - 1),
);
}
}

for i in 0..m - 1 {
for j in 0..n - 1 {
ans = ans.min(
f(0, 0, i, j)
+ f(0, j + 1, i, n - 1)
+ f(i + 1, 0, m - 1, n - 1),
);
ans = ans.min(
f(0, 0, i, n - 1)
+ f(i + 1, 0, m - 1, j)
+ f(i + 1, j + 1, m - 1, n - 1),
);
ans = ans.min(
f(0, 0, i, j)
+ f(i + 1, 0, m - 1, j)
+ f(0, j + 1, m - 1, n - 1),
);
ans = ans.min(
f(0, 0, m - 1, j)
+ f(0, j + 1, i, n - 1)
+ f(i + 1, j + 1, m - 1, n - 1),
);
}
}

ans
}
}
```

#### JavaScript

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var minimumSum = function (grid) {
const m = grid.length;
const n = grid[0].length;
let ans = m * n;
const inf = Number.MAX_SAFE_INTEGER;
const f = (i1, j1, i2, j2) => {
let [x1, y1] = [inf, inf];
let [x2, y2] = [-inf, -inf];
for (let i = i1; i <= i2; i++) {
for (let j = j1; j <= j2; j++) {
if (grid[i][j] === 1) {
x1 = Math.min(x1, i);
y1 = Math.min(y1, j);
x2 = Math.max(x2, i);
y2 = Math.max(y2, j);
}
}
}
return x1 === inf ? 0 : (x2 - x1 + 1) * (y2 - y1 + 1);
};

for (let i1 = 0; i1 < m - 1; i1++) {
for (let i2 = i1 + 1; i2 < m - 1; i2++) {
ans = Math.min(
ans,
f(0, 0, i1, n - 1) + f(i1 + 1, 0, i2, n - 1) + f(i2 + 1, 0, m - 1, n - 1),
);
}
}

for (let j1 = 0; j1 < n - 1; j1++) {
for (let j2 = j1 + 1; j2 < n - 1; j2++) {
ans = Math.min(
ans,
f(0, 0, m - 1, j1) + f(0, j1 + 1, m - 1, j2) + f(0, j2 + 1, m - 1, n - 1),
);
}
}

for (let i = 0; i < m - 1; i++) {
for (let j = 0; j < n - 1; j++) {
ans = Math.min(ans, f(0, 0, i, j) + f(0, j + 1, i, n - 1) + f(i + 1, 0, m - 1, n - 1));
ans = Math.min(
ans,
f(0, 0, i, n - 1) + f(i + 1, 0, m - 1, j) + f(i + 1, j + 1, m - 1, n - 1),
);
ans = Math.min(ans, f(0, 0, i, j) + f(i + 1, 0, m - 1, j) + f(0, j + 1, m - 1, n - 1));
ans = Math.min(
ans,
f(0, 0, m - 1, j) + f(0, j + 1, i, n - 1) + f(i + 1, j + 1, m - 1, n - 1),
);
}
}

return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading