From 1585c1bcd26998d23e3d245ca4d4bbe43d745ee1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 11 Feb 2024 20:14:23 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3033 No.3033.Modify the Matrix --- .../3033.Modify the Matrix/README.md | 111 ++++++++++-------- .../3033.Modify the Matrix/README_EN.md | 111 ++++++++++-------- .../3033.Modify the Matrix/Solution.cpp | 19 ++- .../3033.Modify the Matrix/Solution.cs | 17 +++ .../3033.Modify the Matrix/Solution.go | 19 ++- .../3033.Modify the Matrix/Solution.java | 19 ++- .../3033.Modify the Matrix/Solution.py | 15 +-- .../3033.Modify the Matrix/Solution.ts | 17 ++- 8 files changed, 183 insertions(+), 145 deletions(-) create mode 100644 solution/3000-3099/3033.Modify the Matrix/Solution.cs diff --git a/solution/3000-3099/3033.Modify the Matrix/README.md b/solution/3000-3099/3033.Modify the Matrix/README.md index c831f178950d4..160717e86320b 100644 --- a/solution/3000-3099/3033.Modify the Matrix/README.md +++ b/solution/3000-3099/3033.Modify the Matrix/README.md @@ -44,38 +44,38 @@ ## 解法 -### 方法一 +### 方法一:模拟 + +我们可以根据题目描述,遍历每一列,找到每一列的最大值,然后再遍历每一列,将值为 -1 的元素替换为该列的最大值。 + +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 ```python class Solution: def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]: - rows = len(matrix) - cols = len(matrix[0]) - for i in range(cols): - max_val = float('-inf') - for j in range(rows): - max_val = max(max_val, matrix[j][i]) - for j in range(rows): - if matrix[j][i] == -1: - matrix[j][i] = max_val + m, n = len(matrix), len(matrix[0]) + for j in range(n): + mx = max(matrix[i][j] for i in range(m)) + for i in range(m): + if matrix[i][j] == -1: + matrix[i][j] = mx return matrix ``` ```java class Solution { public int[][] modifiedMatrix(int[][] matrix) { - int r = matrix.length; - int c = matrix[0].length; - for (int i = 0; i < c; i++) { - int maxs = Integer.MIN_VALUE; - for (int j = 0; j < r; j++) { - maxs = Math.max(maxs, matrix[j][i]); + int m = matrix.length, n = matrix[0].length; + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = Math.max(mx, matrix[i][j]); } - for (int j = 0; j < r; j++) { - if (matrix[j][i] == -1) { - matrix[j][i] = maxs; + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; } } } @@ -88,16 +88,15 @@ class Solution { class Solution { public: vector> modifiedMatrix(vector>& matrix) { - int r = matrix.size(); - int c = matrix[0].size(); - for (int i = 0; i < c; i++) { - int maxs = INT_MIN; - for (int j = 0; j < r; j++) { - maxs = max(maxs, matrix[j][i]); + int m = matrix.size(), n = matrix[0].size(); + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = max(mx, matrix[i][j]); } - for (int j = 0; j < r; j++) { - if (matrix[j][i] == -1) { - matrix[j][i] = maxs; + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; } } } @@ -108,18 +107,15 @@ public: ```go func modifiedMatrix(matrix [][]int) [][]int { - r := len(matrix) - c := len(matrix[0]) - for i := 0; i < c; i++ { - maxs := math.MinInt32 - for j := 0; j < r; j++ { - if matrix[j][i] > maxs { - maxs = matrix[j][i] - } + m, n := len(matrix), len(matrix[0]) + for j := 0; j < n; j++ { + mx := -1 + for i := 0; i < m; i++ { + mx = max(mx, matrix[i][j]) } - for j := 0; j < r; j++ { - if matrix[j][i] == -1 { - matrix[j][i] = maxs + for i := 0; i < m; i++ { + if matrix[i][j] == -1 { + matrix[i][j] = mx } } } @@ -129,16 +125,15 @@ func modifiedMatrix(matrix [][]int) [][]int { ```ts function modifiedMatrix(matrix: number[][]): number[][] { - const rows = matrix.length; - const cols = matrix[0].length; - for (let i = 0; i < cols; i++) { - let maxVal = Number.MIN_SAFE_INTEGER; - for (let j = 0; j < rows; j++) { - maxVal = Math.max(maxVal, matrix[j][i]); + const [m, n] = [matrix.length, matrix[0].length]; + for (let j = 0; j < n; ++j) { + let mx = -1; + for (let i = 0; i < m; ++i) { + mx = Math.max(mx, matrix[i][j]); } - for (let j = 0; j < rows; j++) { - if (matrix[j][i] === -1) { - matrix[j][i] = maxVal; + for (let i = 0; i < m; ++i) { + if (matrix[i][j] === -1) { + matrix[i][j] = mx; } } } @@ -146,6 +141,26 @@ function modifiedMatrix(matrix: number[][]): number[][] { } ``` +```cs +public class Solution { + public int[][] ModifiedMatrix(int[][] matrix) { + int m = matrix.Length, n = matrix[0].Length; + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = Math.Max(mx, matrix[i][j]); + } + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; + } + } + } + return matrix; + } +} +``` + diff --git a/solution/3000-3099/3033.Modify the Matrix/README_EN.md b/solution/3000-3099/3033.Modify the Matrix/README_EN.md index 6956f432b6778..4a9f0a42a0db3 100644 --- a/solution/3000-3099/3033.Modify the Matrix/README_EN.md +++ b/solution/3000-3099/3033.Modify the Matrix/README_EN.md @@ -40,38 +40,38 @@ ## Solutions -### Solution 1 +### Solution 1: Simulation + +We can follow the problem description, traverse each column, find the maximum value of each column, and then traverse each column again, replacing the elements with a value of -1 with the maximum value of that column. + +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$. ```python class Solution: def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]: - rows = len(matrix) - cols = len(matrix[0]) - for i in range(cols): - max_val = float('-inf') - for j in range(rows): - max_val = max(max_val, matrix[j][i]) - for j in range(rows): - if matrix[j][i] == -1: - matrix[j][i] = max_val + m, n = len(matrix), len(matrix[0]) + for j in range(n): + mx = max(matrix[i][j] for i in range(m)) + for i in range(m): + if matrix[i][j] == -1: + matrix[i][j] = mx return matrix ``` ```java class Solution { public int[][] modifiedMatrix(int[][] matrix) { - int r = matrix.length; - int c = matrix[0].length; - for (int i = 0; i < c; i++) { - int maxs = Integer.MIN_VALUE; - for (int j = 0; j < r; j++) { - maxs = Math.max(maxs, matrix[j][i]); + int m = matrix.length, n = matrix[0].length; + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = Math.max(mx, matrix[i][j]); } - for (int j = 0; j < r; j++) { - if (matrix[j][i] == -1) { - matrix[j][i] = maxs; + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; } } } @@ -84,16 +84,15 @@ class Solution { class Solution { public: vector> modifiedMatrix(vector>& matrix) { - int r = matrix.size(); - int c = matrix[0].size(); - for (int i = 0; i < c; i++) { - int maxs = INT_MIN; - for (int j = 0; j < r; j++) { - maxs = max(maxs, matrix[j][i]); + int m = matrix.size(), n = matrix[0].size(); + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = max(mx, matrix[i][j]); } - for (int j = 0; j < r; j++) { - if (matrix[j][i] == -1) { - matrix[j][i] = maxs; + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; } } } @@ -104,18 +103,15 @@ public: ```go func modifiedMatrix(matrix [][]int) [][]int { - r := len(matrix) - c := len(matrix[0]) - for i := 0; i < c; i++ { - maxs := math.MinInt32 - for j := 0; j < r; j++ { - if matrix[j][i] > maxs { - maxs = matrix[j][i] - } + m, n := len(matrix), len(matrix[0]) + for j := 0; j < n; j++ { + mx := -1 + for i := 0; i < m; i++ { + mx = max(mx, matrix[i][j]) } - for j := 0; j < r; j++ { - if matrix[j][i] == -1 { - matrix[j][i] = maxs + for i := 0; i < m; i++ { + if matrix[i][j] == -1 { + matrix[i][j] = mx } } } @@ -125,16 +121,15 @@ func modifiedMatrix(matrix [][]int) [][]int { ```ts function modifiedMatrix(matrix: number[][]): number[][] { - const rows = matrix.length; - const cols = matrix[0].length; - for (let i = 0; i < cols; i++) { - let maxVal = Number.MIN_SAFE_INTEGER; - for (let j = 0; j < rows; j++) { - maxVal = Math.max(maxVal, matrix[j][i]); + const [m, n] = [matrix.length, matrix[0].length]; + for (let j = 0; j < n; ++j) { + let mx = -1; + for (let i = 0; i < m; ++i) { + mx = Math.max(mx, matrix[i][j]); } - for (let j = 0; j < rows; j++) { - if (matrix[j][i] === -1) { - matrix[j][i] = maxVal; + for (let i = 0; i < m; ++i) { + if (matrix[i][j] === -1) { + matrix[i][j] = mx; } } } @@ -142,6 +137,26 @@ function modifiedMatrix(matrix: number[][]): number[][] { } ``` +```cs +public class Solution { + public int[][] ModifiedMatrix(int[][] matrix) { + int m = matrix.Length, n = matrix[0].Length; + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = Math.Max(mx, matrix[i][j]); + } + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; + } + } + } + return matrix; + } +} +``` + diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.cpp b/solution/3000-3099/3033.Modify the Matrix/Solution.cpp index 263d96f45bcad..8dc77e95bc55c 100644 --- a/solution/3000-3099/3033.Modify the Matrix/Solution.cpp +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.cpp @@ -1,19 +1,18 @@ class Solution { public: vector> modifiedMatrix(vector>& matrix) { - int r = matrix.size(); - int c = matrix[0].size(); - for (int i = 0; i < c; i++) { - int maxs = INT_MIN; - for (int j = 0; j < r; j++) { - maxs = max(maxs, matrix[j][i]); + int m = matrix.size(), n = matrix[0].size(); + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = max(mx, matrix[i][j]); } - for (int j = 0; j < r; j++) { - if (matrix[j][i] == -1) { - matrix[j][i] = maxs; + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; } } } return matrix; } -}; +}; \ No newline at end of file diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.cs b/solution/3000-3099/3033.Modify the Matrix/Solution.cs new file mode 100644 index 0000000000000..0f5940ad0e6b9 --- /dev/null +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.cs @@ -0,0 +1,17 @@ +public class Solution { + public int[][] ModifiedMatrix(int[][] matrix) { + int m = matrix.Length, n = matrix[0].Length; + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = Math.Max(mx, matrix[i][j]); + } + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; + } + } + } + return matrix; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.go b/solution/3000-3099/3033.Modify the Matrix/Solution.go index f366a197d8d1a..b20cd1fdfc4d1 100644 --- a/solution/3000-3099/3033.Modify the Matrix/Solution.go +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.go @@ -1,16 +1,13 @@ func modifiedMatrix(matrix [][]int) [][]int { - r := len(matrix) - c := len(matrix[0]) - for i := 0; i < c; i++ { - maxs := math.MinInt32 - for j := 0; j < r; j++ { - if matrix[j][i] > maxs { - maxs = matrix[j][i] - } + m, n := len(matrix), len(matrix[0]) + for j := 0; j < n; j++ { + mx := -1 + for i := 0; i < m; i++ { + mx = max(mx, matrix[i][j]) } - for j := 0; j < r; j++ { - if matrix[j][i] == -1 { - matrix[j][i] = maxs + for i := 0; i < m; i++ { + if matrix[i][j] == -1 { + matrix[i][j] = mx } } } diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.java b/solution/3000-3099/3033.Modify the Matrix/Solution.java index 4d81a09ab7fa2..736a2b38ab754 100644 --- a/solution/3000-3099/3033.Modify the Matrix/Solution.java +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.java @@ -1,18 +1,17 @@ class Solution { public int[][] modifiedMatrix(int[][] matrix) { - int r = matrix.length; - int c = matrix[0].length; - for (int i = 0; i < c; i++) { - int maxs = Integer.MIN_VALUE; - for (int j = 0; j < r; j++) { - maxs = Math.max(maxs, matrix[j][i]); + int m = matrix.length, n = matrix[0].length; + for (int j = 0; j < n; ++j) { + int mx = -1; + for (int i = 0; i < m; ++i) { + mx = Math.max(mx, matrix[i][j]); } - for (int j = 0; j < r; j++) { - if (matrix[j][i] == -1) { - matrix[j][i] = maxs; + for (int i = 0; i < m; ++i) { + if (matrix[i][j] == -1) { + matrix[i][j] = mx; } } } return matrix; } -} +} \ No newline at end of file diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.py b/solution/3000-3099/3033.Modify the Matrix/Solution.py index 0044d6d77e59e..d5cf9efd2a9e2 100644 --- a/solution/3000-3099/3033.Modify the Matrix/Solution.py +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.py @@ -1,12 +1,9 @@ class Solution: def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]: - rows = len(matrix) - cols = len(matrix[0]) - for i in range(cols): - max_val = float('-inf') - for j in range(rows): - max_val = max(max_val, matrix[j][i]) - for j in range(rows): - if matrix[j][i] == -1: - matrix[j][i] = max_val + m, n = len(matrix), len(matrix[0]) + for j in range(n): + mx = max(matrix[i][j] for i in range(m)) + for i in range(m): + if matrix[i][j] == -1: + matrix[i][j] = mx return matrix diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.ts b/solution/3000-3099/3033.Modify the Matrix/Solution.ts index f5f295ea6472b..fefe863a98fe2 100644 --- a/solution/3000-3099/3033.Modify the Matrix/Solution.ts +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.ts @@ -1,14 +1,13 @@ function modifiedMatrix(matrix: number[][]): number[][] { - const rows = matrix.length; - const cols = matrix[0].length; - for (let i = 0; i < cols; i++) { - let maxVal = Number.MIN_SAFE_INTEGER; - for (let j = 0; j < rows; j++) { - maxVal = Math.max(maxVal, matrix[j][i]); + const [m, n] = [matrix.length, matrix[0].length]; + for (let j = 0; j < n; ++j) { + let mx = -1; + for (let i = 0; i < m; ++i) { + mx = Math.max(mx, matrix[i][j]); } - for (let j = 0; j < rows; j++) { - if (matrix[j][i] === -1) { - matrix[j][i] = maxVal; + for (let i = 0; i < m; ++i) { + if (matrix[i][j] === -1) { + matrix[i][j] = mx; } } }