Given the following details of a matrix with n
columns and 2
rows :
0
or 1
.upper
.lower
.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 array.
If there are more than one valid solution, any of them will be accepted.
If no valid solution exists, return an empty 2-D array.
Example 1:
Input: upper = 2, lower = 1, colsum = [1,1,1] Output: [[1,1,0],[0,0,1]] Explanation: [[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
Example 2:
Input: upper = 2, lower = 3, colsum = [2,2,1,1] Output: []
Example 3:
Input: upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1] Output: [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
Constraints:
1 <= colsum.length <= 10^5
0 <= upper, lower <= colsum.length
0 <= colsum[i] <= 2
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are given clues about a grid with two rows. The brute force approach is to try every possible arrangement of 0s and 1s in the grid, making sure that the clues we are given hold true for each arrangement. This is done by systematically testing all possibilities.
Here's how the algorithm would work step-by-step:
def reconstruct_matrix_brute_force(upper_value, lower_value, column_sums):
number_of_columns = len(column_sums)
solution = []
def solve(current_matrix, column_index):
nonlocal solution
# If a solution is already found, stop
if solution:
return
# If we've filled all columns, check if the solution is valid
if column_index == number_of_columns:
upper_sum = sum(current_matrix[0])
lower_sum = sum(current_matrix[1])
if upper_sum == upper_value and lower_sum == lower_value:
solution.append([row[:] for row in current_matrix])
return
# Try placing a 0 in the top row
current_matrix[0][column_index] = 0
solve(current_matrix, column_index + 1)
# Try placing a 1 in the top row
current_matrix[0][column_index] = 1
# Prune possibilities if the column sum is greater than 1.
if column_sums[column_index] > 0:
current_matrix[1][column_index] = column_sums[column_index] - current_matrix[0][column_index]
solve(current_matrix, column_index + 1)
# Initialize an empty 2xN matrix
initial_matrix = [[0] * number_of_columns for _ in range(2)]
solve(initial_matrix, 0)
# Return a solution if found, otherwise return an empty list
if solution:
return solution[0]
else:
return []
The goal is to build two rows that add up to a given total while respecting constraints on the sum of each row. We build the matrix in a greedy fashion, prioritizing placing values where they are forced, simplifying the remaining choices.
Here's how the algorithm would work step-by-step:
def reconstruct_matrix(upper_sum, lower_sum, column_sums):
number_of_columns = len(column_sums)
upper_row = [0] * number_of_columns
lower_row = [0] * number_of_columns
for column_index in range(number_of_columns):
if column_sums[column_index] == 2:
# Both rows must be 1 in this column
upper_row[column_index] = 1
lower_row[column_index] = 1
upper_sum -= 1
lower_sum -= 1
elif column_sums[column_index] == 0:
# Both rows must be 0 in this column
upper_row[column_index] = 0
lower_row[column_index] = 0
for column_index in range(number_of_columns):
if column_sums[column_index] == 1:
# Greedily place 1 in the upper row if possible
if upper_sum > 0:
upper_row[column_index] = 1
lower_row[column_index] = 0
upper_sum -= 1
elif lower_sum > 0:
lower_row[column_index] = 1
upper_row[column_index] = 0
lower_sum -= 1
else:
return []
# Check if the row sums match the constraints
if upper_sum == 0 and lower_sum == 0:
return [upper_row, lower_row]
else:
return []
Case | How to Handle |
---|---|
Null or empty colsum array | Return an empty list immediately, as no matrix can be constructed. |
upper or lower is negative | Return an empty list because ones cannot be negative. |
Sum of colsum is not equal to upper + lower | Return an empty list because the total number of ones doesn't match the constraints. |
colsum contains values other than 0, 1, or 2 | Return an empty list as the column sums can only be 0, 1, or 2. |
Impossible case: too many 2's for the available upper/lower values | Return an empty list if the count of 2's in colsum exceeds min(upper, lower). |
Impossible case: too many 1's and insufficient remaining upper/lower values after accommodating 2's. | Return an empty list if allocating 1's after handling 2's and 0's results in upper or lower becoming negative. |
upper or lower greater than the length of colsum | Return an empty list as it's impossible to have more ones than columns. |
Very large colsum array (scalability check) | The solution should iterate through colsum only once, making it O(n) and scalable. |