Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 1605. Find Valid Matrix Given Row and Column Sums #166

Open
plh97 opened this issue Oct 4, 2020 · 0 comments
Open

[LeetCode] 1605. Find Valid Matrix Given Row and Column Sums #166

plh97 opened this issue Oct 4, 2020 · 0 comments
Assignees
Labels
algorithm algorithm javaScript 关于js的一些事

Comments

@plh97
Copy link
Owner

plh97 commented Oct 4, 2020

https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/

给你两个非负整数数组 rowSum 和 colSum ,其中 rowSum[i] 是二维矩阵中第 i 行元素的和, colSum[j] 是第 j 列元素的和。换言之你不知道矩阵里的每个元素,但是你知道每一行和每一列的和。

请找到大小为 rowSum.length x colSum.length 的任意 非负整数 矩阵,且该矩阵满足 rowSum 和 colSum 的要求。

请你返回任意一个满足题目要求的二维矩阵,题目保证存在 至少一个 可行矩阵。

示例 1:

输入:rowSum = [3,8], colSum = [4,7]
输出:[[3,0],[1,7]]
解释:
第 0 行:3 + 0 = 0 == rowSum[0]
第 1 行:1 + 7 = 8 == rowSum[1]
第 0 列:3 + 1 = 4 == colSum[0]
第 1 列:0 + 7 = 7 == colSum[1]
行和列的和都满足题目要求,且所有矩阵元素都是非负的。另一个可行的矩阵为:[[1,2],[3,5]]

示例 2:

输入:rowSum = [5,7,10], colSum = [8,6,8]
输出:[[0,5,0],[6,1,0],[2,0,8]]

示例 3:

输入:rowSum = [14,9], colSum = [6,9,8]
输出:[[0,9,5], [6,0,3]]

示例 4:

输入:rowSum = [1,0], colSum = [1]
输出:[[1], [0]]

示例 5:

输入:rowSum = [0], colSum = [0]
输出:[[0]]

提示:

  • 1 <= rowSum.length, colSum.length <= 500
  • 0 <= rowSum[i], colSum[i] <= 108
  • sum(rows) == sum(columns)

答案

解法1 . 全排列 超时

解法2 贪心算法

基于以下两个理由:

  1. 由于题目的特殊性, 仅仅只需要罗列出一个答案,

  2. 任意一个节点其实就是 Math.min(rowSum, colSum), 然后依次减去当前选取的值.

    • rowSum[i]-=res[i][j]
    • colSum[j]-=res[i][j]
/**
 * @param {number[]} rowSum
 * @param {number[]} colSum
 * @return {number[][]}
 */
var restoreMatrix = function(rowSum, colSum) {
    let res = Array(rowSum.length).fill(0).map(e=>Array(colSum.length).fill(0))
    for(let i=0;i<rowSum.length;i++) {
        for(let j=0;j<colSum.length;j++) {
            res[i][j] = Math.min(rowSum[i], colSum[j])
            rowSum[i]-=res[i][j]
            colSum[j]-=res[i][j]
        }
    }
    return res
};
@plh97 plh97 added javaScript 关于js的一些事 algorithm algorithm labels Oct 4, 2020
@plh97 plh97 self-assigned this Oct 4, 2020
@plh97 plh97 changed the title 1605. Find Valid Matrix Given Row and Column Sums [LeetCode] 1605. Find Valid Matrix Given Row and Column Sums Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm algorithm javaScript 关于js的一些事
Projects
None yet
Development

No branches or pull requests

1 participant