7 ms Beats 93.10%
Intuition\n Describe your first thoughts on how to solve this problem. \nGreedy algorithm.\n\n# Approach\n Describe your approach to solving the problem. \n\nIt
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGreedy algorithm.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nIterate the `colsum` array.\n\n* Case 1: sum = 0. No number to fill in. Skip.\n* Case 2: sum = 1. Choose to fill in the row that has larger row sum.\n* Case 3: sum = 2. Have to fill in two rows.\n\nOperation fill in is done by set value to `1` and decrease the row sum. If row sum is lower than zero, return empty array.\n\n# Complexity\n- Time complexity: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {\n        List<List<Integer>> result = new ArrayList<>();\n        final int N = colsum.length;\n        Integer[] row1 = new Integer[N];\n        Arrays.fill(row1, 0);\n        Integer[] row2 = new Integer[N];\n        Arrays.fill(row2, 0);\n\n        for (int i = 0; i < N; ++i) {\n            if (colsum[i] == 2) {\n                --upper;\n                if (upper < 0) {\n                    return result;\n                }\n                row1[i] = 1;\n                --lower;\n                if (lower < 0) {\n                    return result;\n                }\n                row2[i] = 1;\n            } else if (colsum[i] == 1) {\n                if (upper > lower) {\n                    --upper;\n                    if (upper < 0) {\n                        return result;\n                    }\n                    row1[i] = 1;\n                } else {\n                    --lower;\n                    if (lower < 0) {\n                        return result;\n                    }\n                    row2[i] = 1;\n                }\n            }\n        }\n\n        if (upper != 0 || lower != 0) {\n            return result;\n        }\n\n        result.add(Arrays.asList(row1));\n        result.add(Arrays.asList(row2));\n        return result;\n    }\n}\n```
['Java']