https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums
1605. Find Valid Matrix Given Row and Column Sums
※ 引述《sustainer123 (caster )》之铭言:
: ※ 引述《DJYOSHITAKA (franchouchouISBEST)》之铭言:
: : 看你们说
: : 把最大能填的填进去
: : 我填了 旧过了
: : ==
: : def restoreMatrix(self, rowSum: List[int], colSum: List[int]) ->
: : List[List[int]]:
: : m, n = len(rowSum), len(colSum)
: : ans = [[0 for _ in range(n)] for _ in range(m)]
: : for i in range(m):
: : for j in range(n):
: : ans[i][j] = min(rowSum[i], colSum[j])
: : rowSum[i] -= ans[i][j]
: : colSum[j] -= ans[i][j]
: : return ans
: 思路:
: 看板上作法
: 填最小的 然后把填进去的扣掉
: Python Code:
: class Solution:
: def restoreMatrix(self, rowSum: List[int], colSum: List[int]) ->
: List[List[int]]:
: m = len(rowSum)
: n = len(colSum)
: result = [[0] * n for _ in range(m)]
: for i in range(m):
: for j in range(n):
: result[i][j] = min(rowSum[i],colSum[j])
: rowSum[i] -= result[i][j]
: colSum[j] -= result[i][j]
: return result
思路:
看板上思路
填最小的 然后把填进去的扣掉
Python Code:
class Solution:
def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
rows = len(rowSum)
cols = len(colSum)
matrix = [[0] * cols for _ in range(rows)]
for i in range(rows):
for j in range(cols):
max_possible = min(rowSum[i], colSum[j])
matrix[i][j] = max_possible
rowSum[i] -= max_possible
colSum[j] -= max_possible
return matrix