image_2021-10-23_16-26-06.png
49.8 KB
#medium
#N1605. Find Valid Matrix Given Row and Column Sums
problem link=>https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/
#solution
#N1605. Find Valid Matrix Given Row and Column Sums
problem link=>https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/
#solution
class Solution {
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
int rowLen = rowSum.length;
int colLen = colSum.length;
int[][] ans = new int[rowLen][colLen];
int row = 0, col = 0;
while (row < rowLen && col < colLen) {
int min = Math.min(colSum[col], rowSum[row]);
ans[row][col] = min;
colSum[col] -= min;
rowSum[row] -= min;
if (rowSum[row] == 0)
row++;
if (colSum[col] == 0)
col++;
}
return ans;
}
}