Re: [閒聊] 每日leetcode
看板Marginalman作者JerryChungYC (JerryChung)時間1年前 (2024/07/20 21:01)推噓1(1推 0噓 0→)留言1則, 1人參與討論串533/1548 (看更多)
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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.7.156 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721480479.A.BF0.html
推
07/20 21:01,
1年前
, 1F
07/20 21:01, 1F
※ 編輯: JerryChungYC (114.45.7.156 臺灣), 07/20/2024 21:02:56
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 533 之 1548 篇):