Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 1605. Find Valid Matrix Given Row and Column Sums #223

Open
Woodyiiiiiii opened this issue Mar 14, 2023 · 0 comments
Open

Leetcode 1605. Find Valid Matrix Given Row and Column Sums #223

Woodyiiiiiii opened this issue Mar 14, 2023 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

这道题就是贪心。一般这种类型也的确是要用贪心,没有DP也没有其他方法。

每个单元都充分利用colSum和rowSum。然后更新colSum或者rowSum。

class Solution {
    public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
        int m = rowSum.length, n = colSum.length;
        int[][] ans = new int[m][n];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                ans[i][j] = Math.min(rowSum[i], colSum[j]);
                rowSum[i] -= ans[i][j];
                colSum[j] -= ans[i][j];
            }
        }
        return ans;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant