{"id":421,"date":"2020-09-26T10:39:33","date_gmt":"2020-09-26T02:39:33","guid":{"rendered":"http:\/\/39.96.58.60\/?p=421"},"modified":"2022-10-18T16:39:23","modified_gmt":"2022-10-18T08:39:23","slug":"leetcode-1605-%e7%bb%99%e5%ae%9a%e8%a1%8c%e5%92%8c%e5%88%97%e7%9a%84%e5%92%8c%e6%b1%82%e5%8f%af%e8%a1%8c%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=421","title":{"rendered":"Leetcode 1605. \u7ed9\u5b9a\u884c\u548c\u5217\u7684\u548c\u6c42\u53ef\u884c\u77e9\u9635"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e24\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4&nbsp;<code>rowSum<\/code> \u548c&nbsp;<code>colSum<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>rowSum[i]<\/code>&nbsp;\u662f\u4e8c\u7ef4\u77e9\u9635\u4e2d\u7b2c <code>i<\/code>&nbsp;\u884c\u5143\u7d20\u7684\u548c\uff0c <code>colSum[j]<\/code>&nbsp;\u662f\u7b2c <code>j<\/code>&nbsp;\u5217\u5143\u7d20\u7684\u548c\u3002\u6362\u8a00\u4e4b\u4f60\u4e0d\u77e5\u9053\u77e9\u9635\u91cc\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f46\u662f\u4f60\u77e5\u9053\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\u7684\u548c\u3002<\/p>\n<p>\u8bf7\u627e\u5230\u5927\u5c0f\u4e3a&nbsp;<code>rowSum.length x colSum.length<\/code>&nbsp;\u7684\u4efb\u610f <strong>\u975e\u8d1f\u6574\u6570<\/strong>&nbsp;\u77e9\u9635\uff0c\u4e14\u8be5\u77e9\u9635\u6ee1\u8db3&nbsp;<code>rowSum<\/code> \u548c&nbsp;<code>colSum<\/code>&nbsp;\u7684\u8981\u6c42\u3002<\/p>\n<p>\u8bf7\u4f60\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u4e8c\u7ef4\u77e9\u9635\uff0c\u9898\u76ee\u4fdd\u8bc1\u5b58\u5728 <strong>\u81f3\u5c11\u4e00\u4e2a<\/strong>&nbsp;\u53ef\u884c\u77e9\u9635\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>rowSum = [3,8], colSum = [4,7]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[3,0],\n\n      [1,7]]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\n\u7b2c 0 \u884c\uff1a3 + 0 = 0 == rowSum[0]\n\n\u7b2c 1 \u884c\uff1a1 + 7 = 8 == rowSum[1]\n\n\u7b2c 0 \u5217\uff1a3 + 1 = 4 == colSum[0]\n\n\u7b2c 1 \u5217\uff1a0 + 7 = 7 == colSum[1]\n\n\u884c\u548c\u5217\u7684\u548c\u90fd\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\uff0c\u4e14\u6240\u6709\u77e9\u9635\u5143\u7d20\u90fd\u662f\u975e\u8d1f\u7684\u3002\n\n\u53e6\u4e00\u4e2a\u53ef\u884c\u7684\u77e9\u9635\u4e3a\uff1a[[1,2],\n\n                  [3,5]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>rowSum = [5,7,10], colSum = [8,6,8]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[0,5,0],\n\n      [6,1,0],\n\n      [2,0,8]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>rowSum = [14,9], colSum = [6,9,8]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[0,9,5],\n\n      [6,0,3]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>rowSum = [1,0], colSum = [1]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1],\n\n      [0]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>rowSum = [0], colSum = [0]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[0]]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= rowSum.length, colSum.length &lt;= 500<\/code><\/li>\n<li><code>0 &lt;= rowSum[i], colSum[i] &lt;= 10<sup>8<\/sup><\/code><\/li>\n<li><code>sum(rows) == sum(columns)<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u8d2a\u5fc3\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a96 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8690.00% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a18.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8613.51% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u8d2a\u5fc3\u7b97\u6cd5\n    \u5728\u6bcf\u4e2a\u7a7a\u7f3a\u5904\uff0c\u586b\u53ef\u4ee5\u586b\u7684\u6700\u5927\u503c\n    \u5728\u6bcf\u4e2a\u7a7a\u7f3a\u5904\u586b\u5165\u884c\u5217\u548c\u4e2d\u7684\u6700\u5c0f\u503c\uff0c\u5219\u586b\u5b8c\u540e\uff0c\u5fc5\u7136\u4f1a\u6709\u884c\u5217\u548c\u4e2d\u7684\u67d0\u4e00\u503c\u4e3a0\uff0c\u53ef\u4ee5\u76f4\u63a5\u8df3\u8fc7\u8be5\u884c\/\u5217\uff0c\u63d0\u9ad8\u6548\u7387\n\"\"\"\nclass Solution:\n    def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -&gt; List[List[int]]:\n        r, c = len(rowSum), len(colSum)\n        result = [[0 for _ in range(c)] for _ in range(r)]\n        i, j = 0, 0\n        while i &lt; r and j &lt; c:\n            val = min(rowSum[i], colSum[j]) # \u53d6\u5f53\u524d\u7a7a\u4f4d\u5bf9\u5e94\u884c\u5217\u503c\u7684\u6700\u5c0f\u503c\uff0c\u586b\u5165\u8be5\u7a7a\u4f4d\n            result[i][j] = val\n\n            rowSum[i] -= val    # \u56e0\u4e3a\u586b\u7684\u662f\u5f53\u524d\u884c\u5217\u4e2d\u6700\u5c0f\u7684\u90a3\u4e2a\uff0c\u5219\u6bcf\u6b21\u586b\u5b8c\uff0c\u5219\u5fc5\u6709\u884c\u5217\u4e4b\u4e00\u7684\u548c\u4e3a0\uff0c\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u884c\u5217\u5373\u53ef\n            if rowSum[i] == 0:\n                i += 1\n\n            colSum[j] -= val\n            if colSum[j] == 0:\n                j += 1\n\n        return result\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a324 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8674.59% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a18.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8617.57% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u8d2a\u5fc3\u7b97\u6cd5\n    \u5728\u6bcf\u4e2a\u7a7a\u7f3a\u5904\uff0c\u586b\u53ef\u4ee5\u586b\u7684\u6700\u5927\u503c\n\"\"\"\nclass Solution:\n    def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -&gt; List[List[int]]:\n        r, c = len(rowSum), len(colSum)\n        result = [[0 for _ in range(c)] for _ in range(r)]\n        for i in range(r):\n            for j in range(c):\n                val = min(rowSum[i], colSum[j]) # \u53d6\u5f53\u524d\u7a7a\u4f4d\u5bf9\u5e94\u884c\u5217\u503c\u7684\u6700\u5c0f\u503c\uff0c\u586b\u5165\u8be5\u7a7a\u4f4d\n                result[i][j] = val\n                rowSum[i] -= val    # \u66f4\u65b0\u5f53\u524d\u884c\u5217\u7684\u548c\u7684\u503c\n                colSum[j] -= val\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e24\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4&nbsp;rowSum \u548c&nbsp;colSum&nbsp;\uff0c\u5176\u4e2d&nbsp;rowS&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/421"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":422,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions\/422"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}