package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/
 */
public class Problem1253 {
    public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
        List<List<Integer>> answer = new ArrayList<>();
        answer.add(new ArrayList<>());
        answer.add(new ArrayList<>());
        int twoCounts = 0;
        for (int i = 0; i < colsum.length; i++) {
            if (colsum[i] == 2) {
                answer.get(0).add(1);
                answer.get(1).add(1);
                twoCounts++;
            } else {
                answer.get(0).add(0);
                answer.get(1).add(0);
            }
        }
        int up = upper - twoCounts;
        int lo = lower - twoCounts;
        for (int i = 0; i < colsum.length; i++) {
            if (colsum[i] == 1) {
                if (up > 0) {
                    answer.get(0).set(i, 1);
                    up--;
                } else if (lo > 0) {
                    answer.get(1).set(i, 1);
                    lo--;
                } else {
                    return Collections.emptyList();
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1253 prob = new Problem1253();
//        System.out.println(prob.reconstructMatrix(2, 1, new int[]{1, 1, 1})); // [[1,1,0],[0,0,1]]
//        System.out.println(prob.reconstructMatrix(2, 3, new int[]{2,2,1,1})); // []
//        System.out.println(prob.reconstructMatrix(5, 5, new int[]{2,1,2,0,1,0,1,2,0,1})); // [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
        System.out.println(prob.reconstructMatrix(1, 4, new int[]{2,1,2,0,0,2})); // [[1,0,1,0,0,1],[1,1,1,0,0,1]]
    }
}

@@ -434,21 +434,42 @@
 int 
-twoCounts = 0
+up = upper;%0A        int lo = lower
 ;%0A  
@@ -646,20 +646,145 @@
     
-twoCounts++;
+up--;%0A                lo--;%0A                if (up %3C 0 %7C%7C lo %3C 0) %7B%0A                    return Collections.emptyList();%0A                %7D
 %0A   
@@ -905,80 +905,8 @@
   %7D%0A
-        int up = upper - twoCounts;%0A        int lo = lower - twoCounts;%0A
     
@@ -1332,556 +1332,62 @@
 urn 
-answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1253 prob = new Problem1253();%0A//        System.out.println(prob.reconstructMatrix(2, 1, new int%5B%5D%7B1, 1, 1%7D)); // %5B%5B1,1,0%5D,%5B0,0,1%5D%5D%0A//        System.out.println(prob.reconstructMatrix(2, 3, new int%5B%5D%7B2,2,1,1%7D)); // %5B%5D%0A//        System.out.println(prob.reconstructMatrix(5, 5, new int%5B%5D%7B2,1,2,0,1,0,1,2,0,1%7D)); // %5B%5B1,1,1,0,1,0,0,1,0,0%5D,%5B1,0,1,0,0,0,1,1,0,1%5D%5D%0A        System.out.println(prob.reconstructMatrix(1, 4, new int%5B%5D%7B2,1,2,0,0,2%7D)); // %5B%5B1,0,1,0,0,1%5D,%5B1,1,1,0,0,1%5D%5D
+up == 0 && lo == 0 ? answer : Collections.emptyList();
 %0A   

Solve problem 1253