1605:给定行和列的和求可行矩阵
This commit is contained in:
parent
d3da7e9468
commit
06c7534dbb
@ -0,0 +1,96 @@
|
|||||||
|
//给你两个非负整数数组 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 = 3 == 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] <= 10⁸
|
||||||
|
// sum(rows) == sum(columns)
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 数组 矩阵 👍 26 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//1605:给定行和列的和求可行矩阵
|
||||||
|
class FindValidMatrixGivenRowAndColumnSums {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new FindValidMatrixGivenRowAndColumnSums().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
|
||||||
|
int[][] arrs = new int[rowSum.length][colSum.length];
|
||||||
|
for (int i = 0; i < rowSum.length; i++) {
|
||||||
|
for (int j = 0; j < colSum.length; j++) {
|
||||||
|
arrs[i][j] = Math.min(rowSum[i],colSum[j]);
|
||||||
|
rowSum[i] -= arrs[i][j];
|
||||||
|
colSum[j] -= arrs[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arrs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
<p>给你两个非负整数数组 <code>rowSum</code> 和 <code>colSum</code> ,其中 <code>rowSum[i]</code> 是二维矩阵中第 <code>i</code> 行元素的和, <code>colSum[j]</code> 是第 <code>j</code> 列元素的和。换言之你不知道矩阵里的每个元素,但是你知道每一行和每一列的和。</p>
|
||||||
|
|
||||||
|
<p>请找到大小为 <code>rowSum.length x colSum.length</code> 的任意 <strong>非负整数</strong> 矩阵,且该矩阵满足 <code>rowSum</code> 和 <code>colSum</code> 的要求。</p>
|
||||||
|
|
||||||
|
<p>请你返回任意一个满足题目要求的二维矩阵,题目保证存在 <strong>至少一个</strong> 可行矩阵。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rowSum = [3,8], colSum = [4,7]
|
||||||
|
<strong>输出:</strong>[[3,0],
|
||||||
|
[1,7]]
|
||||||
|
<strong>解释:</strong>
|
||||||
|
第 0 行:3 + 0 = 3 == rowSum[0]
|
||||||
|
第 1 行:1 + 7 = 8 == rowSum[1]
|
||||||
|
第 0 列:3 + 1 = 4 == colSum[0]
|
||||||
|
第 1 列:0 + 7 = 7 == colSum[1]
|
||||||
|
行和列的和都满足题目要求,且所有矩阵元素都是非负的。
|
||||||
|
另一个可行的矩阵为:[[1,2],
|
||||||
|
[3,5]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rowSum = [5,7,10], colSum = [8,6,8]
|
||||||
|
<strong>输出:</strong>[[0,5,0],
|
||||||
|
[6,1,0],
|
||||||
|
[2,0,8]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rowSum = [14,9], colSum = [6,9,8]
|
||||||
|
<strong>输出:</strong>[[0,9,5],
|
||||||
|
[6,0,3]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rowSum = [1,0], colSum = [1]
|
||||||
|
<strong>输出:</strong>[[1],
|
||||||
|
[0]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 5:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rowSum = [0], colSum = [0]
|
||||||
|
<strong>输出:</strong>[[0]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= rowSum.length, colSum.length <= 500</code></li>
|
||||||
|
<li><code>0 <= rowSum[i], colSum[i] <= 10<sup>8</sup></code></li>
|
||||||
|
<li><code>sum(rows) == sum(columns)</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>矩阵</li></div></div><br><div><li>👍 26</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user