{"id":479,"date":"2020-09-26T10:46:45","date_gmt":"2020-09-26T02:46:45","guid":{"rendered":"http:\/\/39.96.58.60\/?p=479"},"modified":"2022-10-18T16:39:14","modified_gmt":"2022-10-18T08:39:14","slug":"leetcode-368-%e6%9c%80%e5%a4%a7%e6%95%b4%e9%99%a4%e5%ad%90%e9%9b%86","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=479","title":{"rendered":"Leetcode 368. \u6700\u5927\u6574\u9664\u5b50\u96c6"},"content":{"rendered":"<p>\u7ed9\u51fa\u4e00\u4e2a\u7531<strong>\u65e0\u91cd\u590d\u7684<\/strong>\u6b63\u6574\u6570\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u627e\u51fa\u5176\u4e2d\u6700\u5927\u7684\u6574\u9664\u5b50\u96c6\uff0c\u5b50\u96c6\u4e2d\u4efb\u610f\u4e00\u5bf9 (S<sub>i\uff0c<\/sub>S<sub>j<\/sub>) \u90fd\u8981\u6ee1\u8db3\uff1aS<sub>i<\/sub> % S<sub>j<\/sub> = 0 \u6216 S<sub>j<\/sub> % S<sub>i<\/sub> = 0\u3002<\/p>\n<p>\u5982\u679c\u6709\u591a\u4e2a\u76ee\u6807\u5b50\u96c6\uff0c\u8fd4\u56de\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u5747\u53ef\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [1,2,3]\n\n<strong>\u8f93\u51fa:<\/strong> [1,2] (\u5f53\u7136, [1,3] \u4e5f\u6b63\u786e)\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [1,2,4,8]\n\n<strong>\u8f93\u51fa:<\/strong> [1,2,4,8]\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u5b66\u3001 \u52a8\u6001\u89c4\u5212\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\uff1a376 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8674.29% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8657.14% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n\n    \u4f8b\uff1a\n            i\n        1,  2,  3,  4,  6,  7,  8,  9,  12\n        0   1   1   2   2   1   3   2   3\n\n        \u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u5747\u9700\u8981\u4e0e\u6bd4\u5176\u5c0f\u7684\u6570\u8fdb\u884c\u76f8\u9664\u3002\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def largestDivisibleSubset(self, nums: List[int]) -&gt; List[int]:\n        n = len(nums)\n        if n == 0:\n            return []\n        nums.sort()\n        dp = [0 for _ in range(n)]  # \u7528\u4e8e\u8bb0\u5f55\u5f53\u524d\u6570\u5b57\u524d\u7684\u6700\u5927\u6574\u9664\u5b50\u96c6\u957f\u5ea6\n        results = [[num]for num in nums]    # \u7528\u4e8e\u8bb0\u5f55\u5bf9\u5e94\u7684\u6700\u5927\u6574\u9664\u5b50\u96c6\n        index, max_ = 0, 0  # \u7528\u4e8e\u8bb0\u5f55\u6700\u5927\u6574\u9664\u5b50\u96c6\u5bf9\u5e94\u7684\u4e0b\u6807\n        for i in range(1, n):   # \u904d\u5386nums\n            result=[]\n            for j in range(0,i):\n                if nums[i] % nums[j] == 0 and dp[i]<dp[j]+1: dp[i]=\"dp[j]+1\" #=\"\" \u66f4\u65b0\u6700\u5927\u6574\u9664\u5b50\u96c6\u957f\u5ea6=\"\" result=\"results[j]\" results[i]=\"result+[nums[i]]\" \u66f4\u65b0\u6700\u5927\u6574\u9664\u5b50\u96c6=\"\" if=\"\"> max_:                # # \u66f4\u65b0\u6700\u5927\u6574\u9664\u5b50\u96c6\u5bf9\u5e94\u4e0b\u6807\n                max_ = dp[i]\n                index = i\n        return results[index]<\/dp[j]+1:><\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u51fa\u4e00\u4e2a\u7531\u65e0\u91cd\u590d\u7684\u6b63\u6574\u6570\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u627e\u51fa\u5176\u4e2d\u6700\u5927\u7684\u6574\u9664\u5b50\u96c6\uff0c\u5b50\u96c6\u4e2d\u4efb\u610f\u4e00\u5bf9 (Si\uff0cSj) \u90fd\u8981\u6ee1\u8db3\uff1aSi %&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","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\/479"}],"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=479"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/479\/revisions"}],"predecessor-version":[{"id":480,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/479\/revisions\/480"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=479"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}