{"id":7445,"date":"2020-10-03T16:14:45","date_gmt":"2020-10-03T23:14:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7445"},"modified":"2020-10-03T16:39:32","modified_gmt":"2020-10-03T23:39:32","slug":"leetcode-1605-find-valid-matrix-given-row-and-column-sums","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1605-find-valid-matrix-given-row-and-column-sums\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1605. Find Valid Matrix Given Row and Column Sums"},"content":{"rendered":"\n<p>You are given two arrays&nbsp;<code>rowSum<\/code>&nbsp;and&nbsp;<code>colSum<\/code>&nbsp;of non-negative integers where&nbsp;<code>rowSum[i]<\/code>&nbsp;is the sum of the elements in the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;row and&nbsp;<code>colSum[j]<\/code>&nbsp;is the sum of the elements of the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.<\/p>\n\n\n\n<p>Find any matrix of&nbsp;<strong>non-negative<\/strong>&nbsp;integers of size&nbsp;<code>rowSum.length x colSum.length<\/code>&nbsp;that satisfies the&nbsp;<code>rowSum<\/code>&nbsp;and&nbsp;<code>colSum<\/code>&nbsp;requirements.<\/p>\n\n\n\n<p>Return&nbsp;<em>a 2D array representing&nbsp;<strong>any<\/strong>&nbsp;matrix that fulfills the requirements<\/em>. It&#8217;s guaranteed that&nbsp;<strong>at least one&nbsp;<\/strong>matrix that fulfills the requirements exists.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rowSum = [3,8], colSum = [4,7]\n<strong>Output:<\/strong> [[3,0],\n         [1,7]]\n<strong>Explanation:<\/strong>\n0th row: 3 + 0 = 0 == rowSum[0]\n1st row: 1 + 7 = 8 == rowSum[1]\n0th column: 3 + 1 = 4 == colSum[0]\n1st column: 0 + 7 = 7 == colSum[1]\nThe row and column sums match, and all matrix elements are non-negative.\nAnother possible matrix is: [[1,2],\n                             [3,5]]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rowSum = [5,7,10], colSum = [8,6,8]\n<strong>Output:<\/strong> [[0,5,0],\n         [6,1,0],\n         [2,0,8]]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rowSum = [14,9], colSum = [6,9,8]\n<strong>Output:<\/strong> [[0,9,5],\n         [6,0,3]]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rowSum = [1,0], colSum = [1]\n<strong>Output:<\/strong> [[1],\n         [0]]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rowSum = [0], colSum = [0]\n<strong>Output:<\/strong> [[0]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= rowSum.length, colSum.length &lt;= 500<\/code><\/li><li><code>0 &lt;= rowSum[i], colSum[i] &lt;= 10<sup>8<\/sup><\/code><\/li><li><code>sum(rows) == sum(columns)<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Let a = min(row[i], col[j]), m[i][j] = a, row[i] -= a, col[j] -=a<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m*n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {\n    const int m = rowSum.size();\n    const int n = colSum.size();\n    vector<vector<int>> ans(m, vector<int>(n));\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j) {\n        ans[i][j] = min(rowSum[i], colSum[j]);\n        rowSum[i] -= ans[i][j];\n        colSum[j] -= ans[i][j];\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two arrays&nbsp;rowSum&nbsp;and&nbsp;colSum&nbsp;of non-negative integers where&nbsp;rowSum[i]&nbsp;is the sum of the elements in the&nbsp;ith&nbsp;row and&nbsp;colSum[j]&nbsp;is the sum of the elements of the&nbsp;jth&nbsp;column of a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,216,177],"class_list":["post-7445","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-matrix","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7445"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7445\/revisions"}],"predecessor-version":[{"id":7447,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7445\/revisions\/7447"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}