diff --git a/solution/3000-3099/3033.Modify the Matrix/README.md b/solution/3000-3099/3033.Modify the Matrix/README.md index ad26409e94e7a..c831f178950d4 100644 --- a/solution/3000-3099/3033.Modify the Matrix/README.md +++ b/solution/3000-3099/3033.Modify the Matrix/README.md @@ -49,19 +49,101 @@ ```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 + 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]); + } + for (int j = 0; j < r; j++) { + if (matrix[j][i] == -1) { + matrix[j][i] = maxs; + } + } + } + return matrix; + } +} ``` ```cpp - +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]); + } + for (int j = 0; j < r; j++) { + if (matrix[j][i] == -1) { + matrix[j][i] = maxs; + } + } + } + return matrix; + } +}; ``` ```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] + } + } + for j := 0; j < r; j++ { + if matrix[j][i] == -1 { + matrix[j][i] = maxs + } + } + } + return matrix +} +``` +```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]); + } + for (let j = 0; j < rows; j++) { + if (matrix[j][i] === -1) { + matrix[j][i] = maxVal; + } + } + } + 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 4ff7ee3bc3b14..6956f432b6778 100644 --- a/solution/3000-3099/3033.Modify the Matrix/README_EN.md +++ b/solution/3000-3099/3033.Modify the Matrix/README_EN.md @@ -45,19 +45,101 @@ ```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 + 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]); + } + for (int j = 0; j < r; j++) { + if (matrix[j][i] == -1) { + matrix[j][i] = maxs; + } + } + } + return matrix; + } +} ``` ```cpp - +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]); + } + for (int j = 0; j < r; j++) { + if (matrix[j][i] == -1) { + matrix[j][i] = maxs; + } + } + } + return matrix; + } +}; ``` ```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] + } + } + for j := 0; j < r; j++ { + if matrix[j][i] == -1 { + matrix[j][i] = maxs + } + } + } + return matrix +} +``` +```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]); + } + for (let j = 0; j < rows; j++) { + if (matrix[j][i] === -1) { + matrix[j][i] = maxVal; + } + } + } + return matrix; +} ``` diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.cpp b/solution/3000-3099/3033.Modify the Matrix/Solution.cpp new file mode 100644 index 0000000000000..263d96f45bcad --- /dev/null +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.cpp @@ -0,0 +1,19 @@ +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]); + } + for (int j = 0; j < r; j++) { + if (matrix[j][i] == -1) { + matrix[j][i] = maxs; + } + } + } + return matrix; + } +}; diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.go b/solution/3000-3099/3033.Modify the Matrix/Solution.go new file mode 100644 index 0000000000000..f366a197d8d1a --- /dev/null +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.go @@ -0,0 +1,18 @@ +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] + } + } + for j := 0; j < r; j++ { + if matrix[j][i] == -1 { + matrix[j][i] = maxs + } + } + } + return matrix +} diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.java b/solution/3000-3099/3033.Modify the Matrix/Solution.java new file mode 100644 index 0000000000000..4d81a09ab7fa2 --- /dev/null +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.java @@ -0,0 +1,18 @@ +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]); + } + for (int j = 0; j < r; j++) { + if (matrix[j][i] == -1) { + matrix[j][i] = maxs; + } + } + } + return matrix; + } +} diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.py b/solution/3000-3099/3033.Modify the Matrix/Solution.py new file mode 100644 index 0000000000000..0044d6d77e59e --- /dev/null +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.py @@ -0,0 +1,12 @@ +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 + return matrix diff --git a/solution/3000-3099/3033.Modify the Matrix/Solution.ts b/solution/3000-3099/3033.Modify the Matrix/Solution.ts new file mode 100644 index 0000000000000..f5f295ea6472b --- /dev/null +++ b/solution/3000-3099/3033.Modify the Matrix/Solution.ts @@ -0,0 +1,16 @@ +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]); + } + for (let j = 0; j < rows; j++) { + if (matrix[j][i] === -1) { + matrix[j][i] = maxVal; + } + } + } + return matrix; +}