Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0598 (#4011)
Browse files Browse the repository at this point in the history
No.0598.Range Addition II
  • Loading branch information
yanglbme authored Feb 2, 2025
1 parent 4faa639 commit e100c8b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 5 deletions.
52 changes: 51 additions & 1 deletion solution/0500-0599/0598.Range Addition II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:脑筋急转弯

我们注意到,所有操作子矩阵的交集就是最终的最大整数所在的子矩阵,并且每个操作子矩阵都是从左上角 $(0, 0)$ 开始的,因此,我们遍历所有操作子矩阵,求出行数和列数的最小值,最后返回这两个值的乘积即可。

注意,如果操作数组为空,那么矩阵中的最大整数个数就是 $m \times n$。

时间复杂度 $O(k)$,其中 $k$ 是操作数组 $\textit{ops}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -123,6 +129,50 @@ func maxCount(m int, n int, ops [][]int) int {
}
```

#### TypeScript

```ts
function maxCount(m: number, n: number, ops: number[][]): number {
for (const [a, b] of ops) {
m = Math.min(m, a);
n = Math.min(n, b);
}
return m * n;
}
```

#### Rust

```rust
impl Solution {
pub fn max_count(mut m: i32, mut n: i32, ops: Vec<Vec<i32>>) -> i32 {
for op in ops {
m = m.min(op[0]);
n = n.min(op[1]);
}
m * n
}
}
```

#### JavaScript

```js
/**
* @param {number} m
* @param {number} n
* @param {number[][]} ops
* @return {number}
*/
var maxCount = function (m, n, ops) {
for (const [a, b] of ops) {
m = Math.min(m, a);
n = Math.min(n, b);
}
return m * n;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
54 changes: 52 additions & 2 deletions solution/0500-0599/0598.Range Addition II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Brain Teaser

We notice that the intersection of all operation submatrices is the submatrix where the final maximum integer is located, and each operation submatrix starts from the top-left corner $(0, 0)$. Therefore, we traverse all operation submatrices to find the minimum number of rows and columns. Finally, we return the product of these two values.

Note that if the operation array is empty, the number of maximum integers in the matrix is $m \times n$.

The time complexity is $O(k)$, where $k$ is the length of the operation array $\textit{ops}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -96,7 +102,7 @@ class Solution {
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
for (auto op : ops) {
for (const auto& op : ops) {
m = min(m, op[0]);
n = min(n, op[1]);
}
Expand All @@ -117,6 +123,50 @@ func maxCount(m int, n int, ops [][]int) int {
}
```

#### TypeScript

```ts
function maxCount(m: number, n: number, ops: number[][]): number {
for (const [a, b] of ops) {
m = Math.min(m, a);
n = Math.min(n, b);
}
return m * n;
}
```

#### Rust

```rust
impl Solution {
pub fn max_count(mut m: i32, mut n: i32, ops: Vec<Vec<i32>>) -> i32 {
for op in ops {
m = m.min(op[0]);
n = n.min(op[1]);
}
m * n
}
}
```

#### JavaScript

```js
/**
* @param {number} m
* @param {number} n
* @param {number[][]} ops
* @return {number}
*/
var maxCount = function (m, n, ops) {
for (const [a, b] of ops) {
m = Math.min(m, a);
n = Math.min(n, b);
}
return m * n;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
4 changes: 2 additions & 2 deletions solution/0500-0599/0598.Range Addition II/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
for (auto op : ops) {
for (const auto& op : ops) {
m = min(m, op[0]);
n = min(n, op[1]);
}
return m * n;
}
};
};
13 changes: 13 additions & 0 deletions solution/0500-0599/0598.Range Addition II/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {number} m
* @param {number} n
* @param {number[][]} ops
* @return {number}
*/
var maxCount = function (m, n, ops) {
for (const [a, b] of ops) {
m = Math.min(m, a);
n = Math.min(n, b);
}
return m * n;
};
9 changes: 9 additions & 0 deletions solution/0500-0599/0598.Range Addition II/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
impl Solution {
pub fn max_count(mut m: i32, mut n: i32, ops: Vec<Vec<i32>>) -> i32 {
for op in ops {
m = m.min(op[0]);
n = n.min(op[1]);
}
m * n
}
}
7 changes: 7 additions & 0 deletions solution/0500-0599/0598.Range Addition II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function maxCount(m: number, n: number, ops: number[][]): number {
for (const [a, b] of ops) {
m = Math.min(m, a);
n = Math.min(n, b);
}
return m * n;
}

0 comments on commit e100c8b

Please sign in to comment.