Skip to content

Instantly share code, notes, and snippets.

@rusyasoft
Created October 2, 2023 23:54
Show Gist options
  • Save rusyasoft/64d873d042f5e16aa42e299b49acf563 to your computer and use it in GitHub Desktop.
Save rusyasoft/64d873d042f5e16aa42e299b49acf563 to your computer and use it in GitHub Desktop.
1253. Reconstruct a 2-Row Binary Matrix [medium] https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix
/*
https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix
*/
class Solution {
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
int n = colsum.length;
int totalSum = upper + lower;
int[][] resAr = new int[2][n];
int origUpper = upper;
int origLower = lower;
int sumOf2 = 0;
int numOf2s = 0;
int sumOfCols = 0;
for (int i = 0; i < n; i++) {
if (colsum[i] == 2) {
resAr[0][i] = 1;
resAr[1][i] = 1;
upper--;
lower--;
sumOf2 += colsum[i];
numOf2s++;
}
sumOfCols += colsum[i];
}
System.out.println("---- sumOf2: " + sumOf2);
if (totalSum < sumOf2 || sumOfCols != totalSum
|| numOf2s > origUpper || numOf2s > origLower) return new ArrayList<>();
System.out.println("----");
for (int i = 0; i < n; i++) {
if (colsum[i] == 1) {
if (upper > 0) {
resAr[0][i] = 1;
upper--;
} else {
resAr[1][i] = 1;
lower--;
}
}
}
List<List<Integer>> resList = new ArrayList<>();
resList.add(new ArrayList<>());
resList.add(new ArrayList<>());
for (int i = 0; i < n; i++) {
resList.get(0).add(resAr[0][i]);
resList.get(1).add(resAr[1][i]);
}
return resList;
}
}
/**
u: 9
l: 2
[0,1,2,0,0,0,0,0,2,1,2,1,2]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment