Skip to content

Commit d51d4e4

Browse files
committed
feat: add solutions to lc problem: No.1605
No.1605.Find Valid Matrix Given Row and Column Sums
1 parent 93269dc commit d51d4e4

File tree

6 files changed

+222
-2
lines changed

6 files changed

+222
-2
lines changed

solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,91 @@
8383
<!-- 这里可写当前语言的特殊实现逻辑 -->
8484

8585
```python
86-
86+
class Solution:
87+
def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
88+
m, n = len(rowSum), len(colSum)
89+
ans = [[0] * n for _ in range(m)]
90+
for i in range(m):
91+
for j in range(n):
92+
x = min(rowSum[i], colSum[j])
93+
ans[i][j] = x
94+
rowSum[i] -= x
95+
colSum[j] -= x
96+
return ans
8797
```
8898

8999
### **Java**
90100

91101
<!-- 这里可写当前语言的特殊实现逻辑 -->
92102

93103
```java
104+
class Solution {
105+
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
106+
int m = rowSum.length;
107+
int n = colSum.length;
108+
int[][] ans = new int[m][n];
109+
for (int i = 0; i < m; ++i) {
110+
for (int j = 0; j < n; ++j) {
111+
int x = Math.min(rowSum[i], colSum[j]);
112+
ans[i][j] = x;
113+
rowSum[i] -= x;
114+
colSum[j] -= x;
115+
}
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
128+
int m = rowSum.size(), n = colSum.size();
129+
vector<vector<int>> ans(m, vector<int>(n));
130+
for (int i = 0; i < m; ++i)
131+
{
132+
for (int j = 0; j < n; ++j)
133+
{
134+
int x = min(rowSum[i], colSum[j]);
135+
ans[i][j] = x;
136+
rowSum[i] -= x;
137+
colSum[j] -= x;
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
94144
145+
### **Go**
146+
147+
```go
148+
func restoreMatrix(rowSum []int, colSum []int) [][]int {
149+
m, n := len(rowSum), len(colSum)
150+
ans := make([][]int, m)
151+
for i := range ans {
152+
ans[i] = make([]int, n)
153+
}
154+
for i := range rowSum {
155+
for j := range colSum {
156+
x := min(rowSum[i], colSum[j])
157+
ans[i][j] = x
158+
rowSum[i] -= x
159+
colSum[j] -= x
160+
}
161+
}
162+
return ans
163+
}
164+
165+
func min(a, b int) int {
166+
if a < b {
167+
return a
168+
}
169+
return b
170+
}
95171
```
96172

97173
### **...**

solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,89 @@ Another possible matrix is: [[1,2],
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
57+
m, n = len(rowSum), len(colSum)
58+
ans = [[0] * n for _ in range(m)]
59+
for i in range(m):
60+
for j in range(n):
61+
x = min(rowSum[i], colSum[j])
62+
ans[i][j] = x
63+
rowSum[i] -= x
64+
colSum[j] -= x
65+
return ans
5666
```
5767

5868
### **Java**
5969

6070
```java
71+
class Solution {
72+
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
73+
int m = rowSum.length;
74+
int n = colSum.length;
75+
int[][] ans = new int[m][n];
76+
for (int i = 0; i < m; ++i) {
77+
for (int j = 0; j < n; ++j) {
78+
int x = Math.min(rowSum[i], colSum[j]);
79+
ans[i][j] = x;
80+
rowSum[i] -= x;
81+
colSum[j] -= x;
82+
}
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
95+
int m = rowSum.size(), n = colSum.size();
96+
vector<vector<int>> ans(m, vector<int>(n));
97+
for (int i = 0; i < m; ++i)
98+
{
99+
for (int j = 0; j < n; ++j)
100+
{
101+
int x = min(rowSum[i], colSum[j]);
102+
ans[i][j] = x;
103+
rowSum[i] -= x;
104+
colSum[j] -= x;
105+
}
106+
}
107+
return ans;
108+
}
109+
};
110+
```
61111
112+
### **Go**
113+
114+
```go
115+
func restoreMatrix(rowSum []int, colSum []int) [][]int {
116+
m, n := len(rowSum), len(colSum)
117+
ans := make([][]int, m)
118+
for i := range ans {
119+
ans[i] = make([]int, n)
120+
}
121+
for i := range rowSum {
122+
for j := range colSum {
123+
x := min(rowSum[i], colSum[j])
124+
ans[i][j] = x
125+
rowSum[i] -= x
126+
colSum[j] -= x
127+
}
128+
}
129+
return ans
130+
}
131+
132+
func min(a, b int) int {
133+
if a < b {
134+
return a
135+
}
136+
return b
137+
}
62138
```
63139

64140
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
4+
int m = rowSum.size(), n = colSum.size();
5+
vector<vector<int>> ans(m, vector<int>(n));
6+
for (int i = 0; i < m; ++i)
7+
{
8+
for (int j = 0; j < n; ++j)
9+
{
10+
int x = min(rowSum[i], colSum[j]);
11+
ans[i][j] = x;
12+
rowSum[i] -= x;
13+
colSum[j] -= x;
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func restoreMatrix(rowSum []int, colSum []int) [][]int {
2+
m, n := len(rowSum), len(colSum)
3+
ans := make([][]int, m)
4+
for i := range ans {
5+
ans[i] = make([]int, n)
6+
}
7+
for i := range rowSum {
8+
for j := range colSum {
9+
x := min(rowSum[i], colSum[j])
10+
ans[i][j] = x
11+
rowSum[i] -= x
12+
colSum[j] -= x
13+
}
14+
}
15+
return ans
16+
}
17+
18+
func min(a, b int) int {
19+
if a < b {
20+
return a
21+
}
22+
return b
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
3+
int m = rowSum.length;
4+
int n = colSum.length;
5+
int[][] ans = new int[m][n];
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
int x = Math.min(rowSum[i], colSum[j]);
9+
ans[i][j] = x;
10+
rowSum[i] -= x;
11+
colSum[j] -= x;
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
3+
m, n = len(rowSum), len(colSum)
4+
ans = [[0] * n for _ in range(m)]
5+
for i in range(m):
6+
for j in range(n):
7+
x = min(rowSum[i], colSum[j])
8+
ans[i][j] = x
9+
rowSum[i] -= x
10+
colSum[j] -= x
11+
return ans

0 commit comments

Comments
 (0)