1605. Find Valid Matrix Given Row and Column Sums Medium

@problem@discussion
#Array#Greedy#Matrix



1/**
2 * [1605] Find Valid Matrix Given Row and Column Sums
3 *
4 * You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the i^th row and colSum[j] is the sum of the elements of the j^th column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.
5 * Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies the rowSum and colSum requirements.
6 * Return a 2D array representing any matrix that fulfills the requirements. It's guaranteed that at least one matrix that fulfills the requirements exists.
7 *  
8 * Example 1:
9 * 
10 * Input: rowSum = [3,8], colSum = [4,7]
11 * Output: [[3,0],
12 *          [1,7]]
13 * Explanation: 
14 * 0^th row: 3 + 0 = 3 == rowSum[0]
15 * 1^st row: 1 + 7 = 8 == rowSum[1]
16 * 0^th column: 3 + 1 = 4 == colSum[0]
17 * 1^st column: 0 + 7 = 7 == colSum[1]
18 * The row and column sums match, and all matrix elements are non-negative.
19 * Another possible matrix is: [[1,2],
20 *                              [3,5]]
21 * 
22 * Example 2:
23 * 
24 * Input: rowSum = [5,7,10], colSum = [8,6,8]
25 * Output: [[0,5,0],
26 *          [6,1,0],
27 *          [2,0,8]]
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	1 <= rowSum.length, colSum.length <= 500
33 * 	0 <= rowSum[i], colSum[i] <= 10^8
34 * 	sum(rows) == sum(columns)
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/
40// discuss: https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn restore_matrix(row_sum: Vec<i32>, col_sum: Vec<i32>) -> Vec<Vec<i32>> {
46        vec![]
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_1605() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.