1253. Reconstruct a 2-Row Binary Matrix Medium

@problem@discussion
#Array#Greedy#Matrix



1/**
2 * [1253] Reconstruct a 2-Row Binary Matrix
3 *
4 * Given the following details of a matrix with n columns and 2 rows :
5 * 
6 * 	The matrix is a binary matrix, which means each element in the matrix can be 0 or 1.
7 * 	The sum of elements of the 0-th(upper) row is given as upper.
8 * 	The sum of elements of the 1-st(lower) row is given as lower.
9 * 	The sum of elements in the i-th column(0-indexed) is colsum[i], where colsum is given as an integer array with length n.
10 * 
11 * Your task is to reconstruct the matrix with upper, lower and colsum.
12 * Return it as a 2-D integer array.
13 * If there are more than one valid solution, any of them will be accepted.
14 * If no valid solution exists, return an empty 2-D array.
15 *  
16 * Example 1:
17 * 
18 * Input: upper = 2, lower = 1, colsum = [1,1,1]
19 * Output: [[1,1,0],[0,0,1]]
20 * Explanation: [[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
21 * 
22 * Example 2:
23 * 
24 * Input: upper = 2, lower = 3, colsum = [2,2,1,1]
25 * Output: []
26 * 
27 * Example 3:
28 * 
29 * Input: upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
30 * Output: [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= colsum.length <= 10^5
36 * 	0 <= upper, lower <= colsum.length
37 * 	0 <= colsum[i] <= 2
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/
43// discuss: https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn reconstruct_matrix(upper: i32, lower: i32, colsum: Vec<i32>) -> Vec<Vec<i32>> {
49        vec![]
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1253() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.