Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 731 Bytes

1605. 给定行和列的和求可行矩阵.md

File metadata and controls

25 lines (18 loc) · 731 Bytes
  • 脑筋急转弯
function restoreMatrix(rowSum: number[], colSum: number[]): number[][] {

    const results: number[][] = new Array(rowSum.length).fill(null).map(() => new Array(colSum.length).fill(0));

    for (let row = 0; row < rowSum.length; ++row) {
        for (let col = 0; col < colSum.length; ++col) {
            results[row][col] = Math.min(rowSum[row], colSum[col]);
            rowSum[row] -= results[row][col];
            colSum[col] -= results[row][col];

            if (!rowSum[row]) {
                break;
            }
        }
    }

    return results;
};