Reconstruct a 2-Row Binary Matrix - Complete Solution Guide
Reconstruct a 2-Row Binary Matrix is LeetCode problem 1253, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the following details of a matrix with n columns and 2 rows : The matrix is a binary matrix, which means each element in the matrix can be 0 or 1 . The sum of elements of the 0-th(upper) row is given as upper . The sum of elements of the 1-st(lower) row is given as lower . 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 . Your task is to reconstruct the matrix with upper , lower and colsum . Return it as a 2-D integer
Detailed Explanation
The problem requires reconstructing a 2xN binary matrix given the sum of each row (upper and lower) and the sum of each column (colsum). Each element in the matrix can only be 0 or 1. The goal is to return *any* valid matrix that satisfies these constraints. If no valid matrix exists, return an empty 2D array. Constraints: colsum length <= 10^5, upper/lower <= colsum length, colsum[i] can be 0, 1, or 2.
Solution Approach
The provided solution utilizes a greedy approach. First, it verifies that the sum of the 'upper' and 'lower' constraints equals the sum of the 'colsum' array. If not, an empty matrix is returned. Then, it iterates through the 'colsum' array. If a column's sum is 2, both corresponding entries in the result matrix are set to 1, and 'upper' and 'lower' are decremented. If at any point 'upper' or 'lower' become negative, an empty matrix is returned. Finally, it iterates through the 'colsum' array again, and for columns with a sum of 1, it greedily assigns 1 to the upper row if 'upper' is greater than 0, otherwise assigns 1 to the lower row.
Step-by-Step Algorithm
- Step 1: Check if the sum of 'upper' and 'lower' equals the sum of 'colsum'. If not, return an empty matrix (no solution possible).
- Step 2: Initialize a 2xN matrix 'res' with all elements set to 0.
- Step 3: Iterate through 'colsum'. For each column 'i':
- Step 4: If colsum[i] == 2, set res[0][i] = 1 and res[1][i] = 1. Decrement 'upper' and 'lower'.
- Step 5: If upper < 0 or lower < 0 after assigning all colsum == 2, return empty matrix (no solution).
- Step 6: Iterate through 'colsum' again. For each column 'i':
- Step 7: If colsum[i] == 1, if upper > 0, set res[0][i] = 1 and decrement 'upper'. Otherwise, set res[1][i] = 1 and decrement 'lower'.
- Step 8: Return the reconstructed matrix 'res'.
Key Insights
- Insight 1: The sum of 'upper' and 'lower' must equal the sum of all elements in 'colsum'; otherwise, no solution exists.
- Insight 2: Columns with a sum of 2 must have 1 in both the upper and lower rows. Columns with a sum of 0 must have 0 in both rows. Columns with a sum of 1 have flexibility: either the upper or lower row can be 1, and the other must be 0.
- Insight 3: A greedy approach can be used by prioritizing filling columns with sum 2 first and then greedily filling the remaining columns with sum 1 to satisfy 'upper' and 'lower' constraints.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Greedy, Matrix.
Companies
Asked at: American Express, Grab.