Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9k
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

feat: add solutions to lc problem: No.0598 #4011

Merged
merged 1 commit into from
Feb 2, 2025
Merged
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
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
@@ -67,7 +67,13 @@ tags:

<!-- solution:start -->

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

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

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

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

<!-- tabs:start -->

@@ -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 -->
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
@@ -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 -->

@@ -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]);
}
@@ -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 -->
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;
}