diff --git a/solution/0500-0599/0598.Range Addition II/README.md b/solution/0500-0599/0598.Range Addition II/README.md index 4031844398e52..44fd37fcb568b 100644 --- a/solution/0500-0599/0598.Range Addition II/README.md +++ b/solution/0500-0599/0598.Range Addition II/README.md @@ -67,7 +67,13 @@ tags: -### 方法一 +### 方法一:脑筋急转弯 + +我们注意到,所有操作子矩阵的交集就是最终的最大整数所在的子矩阵,并且每个操作子矩阵都是从左上角 $(0, 0)$ 开始的,因此,我们遍历所有操作子矩阵,求出行数和列数的最小值,最后返回这两个值的乘积即可。 + +注意,如果操作数组为空,那么矩阵中的最大整数个数就是 $m \times n$。 + +时间复杂度 $O(k)$,其中 $k$ 是操作数组 $\textit{ops}$ 的长度。空间复杂度 $O(1)$。 @@ -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>) -> 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; +}; +``` + diff --git a/solution/0500-0599/0598.Range Addition II/README_EN.md b/solution/0500-0599/0598.Range Addition II/README_EN.md index fd5e5237b9517..7ea839d741336 100644 --- a/solution/0500-0599/0598.Range Addition II/README_EN.md +++ b/solution/0500-0599/0598.Range Addition II/README_EN.md @@ -61,7 +61,13 @@ tags: -### 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)$. @@ -96,7 +102,7 @@ class Solution { class Solution { public: int maxCount(int m, int n, vector>& 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>) -> 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; +}; +``` + diff --git a/solution/0500-0599/0598.Range Addition II/Solution.cpp b/solution/0500-0599/0598.Range Addition II/Solution.cpp index e78047cff9b35..4ed70e314d9ba 100644 --- a/solution/0500-0599/0598.Range Addition II/Solution.cpp +++ b/solution/0500-0599/0598.Range Addition II/Solution.cpp @@ -1,10 +1,10 @@ class Solution { public: int maxCount(int m, int n, vector>& ops) { - for (auto op : ops) { + for (const auto& op : ops) { m = min(m, op[0]); n = min(n, op[1]); } return m * n; } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0598.Range Addition II/Solution.js b/solution/0500-0599/0598.Range Addition II/Solution.js new file mode 100644 index 0000000000000..cbb4cb3a71203 --- /dev/null +++ b/solution/0500-0599/0598.Range Addition II/Solution.js @@ -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; +}; diff --git a/solution/0500-0599/0598.Range Addition II/Solution.rs b/solution/0500-0599/0598.Range Addition II/Solution.rs new file mode 100644 index 0000000000000..152e6066f7dcc --- /dev/null +++ b/solution/0500-0599/0598.Range Addition II/Solution.rs @@ -0,0 +1,9 @@ +impl Solution { + pub fn max_count(mut m: i32, mut n: i32, ops: Vec>) -> i32 { + for op in ops { + m = m.min(op[0]); + n = n.min(op[1]); + } + m * n + } +} diff --git a/solution/0500-0599/0598.Range Addition II/Solution.ts b/solution/0500-0599/0598.Range Addition II/Solution.ts new file mode 100644 index 0000000000000..3fe338236f681 --- /dev/null +++ b/solution/0500-0599/0598.Range Addition II/Solution.ts @@ -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; +}