Skip to content

Commit 534c4fc

Browse files
committed
Runtime 737 ms (Top 78.91%) | Memory 21.0 MB (Top 6.18%)
1 parent 3abb034 commit 534c4fc

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
# Runtime: 2542 ms (Top 7.54%) | Memory: 19.1 MB (Top 61.04%)
21
class Solution:
3-
def restoreMatrix(self, rs: List[int], cs: List[int]) -> List[List[int]]:
4-
row = len(rs)
5-
col = len(cs)
6-
ans = [[0 for i in range(col)] for j in range(row)]
7-
for i in range(row):
8-
for j in range(col):
9-
el = min(rs[i], cs[j])
10-
ans[i][j] = el
11-
rs[i] -= el
12-
cs[j] -= el
13-
return ans
2+
def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
3+
matrix = []
4+
for row in range(len(rowSum)):
5+
data, array = rowSum[row], []
6+
for col in range(len(colSum)):
7+
if data == 0 or colSum[col] == 0:
8+
array.append(0)
9+
elif data > colSum[col]:
10+
data -= colSum[col]
11+
array.append(colSum[col])
12+
colSum[col] = 0
13+
else:
14+
array.append(data)
15+
colSum[col] -= data
16+
data = 0
17+
matrix.append(array)
18+
19+
return matrix
20+
21+
22+
23+
24+
25+
26+
27+
28+

0 commit comments

Comments
 (0)