{"id":435,"date":"2020-09-26T10:41:18","date_gmt":"2020-09-26T02:41:18","guid":{"rendered":"http:\/\/39.96.58.60\/?p=435"},"modified":"2022-10-18T16:39:23","modified_gmt":"2022-10-18T08:39:23","slug":"leetcode-375-%e7%8c%9c%e6%95%b0%e5%ad%97%e5%a4%a7%e5%b0%8f-ii","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=435","title":{"rendered":"Leetcode 375. \u731c\u6570\u5b57\u5927\u5c0f II"},"content":{"rendered":"<p>\u6211\u4eec\u6b63\u5728\u73a9\u4e00\u4e2a\u731c\u6570\u6e38\u620f\uff0c\u6e38\u620f\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n<p>\u6211\u4ece&nbsp;<strong>1&nbsp;<\/strong>\u5230 <strong>n<\/strong> \u4e4b\u95f4\u9009\u62e9\u4e00\u4e2a\u6570\u5b57\uff0c\u4f60\u6765\u731c\u6211\u9009\u4e86\u54ea\u4e2a\u6570\u5b57\u3002<\/p>\n<p>\u6bcf\u6b21\u4f60\u731c\u9519\u4e86\uff0c\u6211\u90fd\u4f1a\u544a\u8bc9\u4f60\uff0c\u6211\u9009\u7684\u6570\u5b57\u6bd4\u4f60\u7684\u5927\u4e86\u6216\u8005\u5c0f\u4e86\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5f53\u4f60\u731c\u4e86\u6570\u5b57 x \u5e76\u4e14\u731c\u9519\u4e86\u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u652f\u4ed8\u91d1\u989d\u4e3a x \u7684\u73b0\u91d1\u3002\u76f4\u5230\u4f60\u731c\u5230\u6211\u9009\u7684\u6570\u5b57\uff0c\u4f60\u624d\u7b97\u8d62\u5f97\u4e86\u8fd9\u4e2a\u6e38\u620f\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre>n = 10, \u6211\u9009\u62e9\u4e868.\n\n\n\n\u7b2c\u4e00\u8f6e: \u4f60\u731c\u6211\u9009\u62e9\u7684\u6570\u5b57\u662f5\uff0c\u6211\u4f1a\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5927\u4e00\u4e9b\uff0c\u7136\u540e\u4f60\u9700\u8981\u652f\u4ed85\u5757\u3002\n\n\u7b2c\u4e8c\u8f6e: \u4f60\u731c\u662f7\uff0c\u6211\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5927\u4e00\u4e9b\uff0c\u4f60\u652f\u4ed87\u5757\u3002\n\n\u7b2c\u4e09\u8f6e: \u4f60\u731c\u662f9\uff0c\u6211\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5c0f\u4e00\u4e9b\uff0c\u4f60\u652f\u4ed89\u5757\u3002\n\n\n\n\u6e38\u620f\u7ed3\u675f\u30028 \u5c31\u662f\u6211\u9009\u7684\u6570\u5b57\u3002\n\n\n\n\u4f60\u6700\u7ec8\u8981\u652f\u4ed8 5 + 7 + 9 = 21 \u5757\u94b1\u3002\n\n<\/pre>\n<p>\u7ed9\u5b9a&nbsp;<strong>n \u2265 1\uff0c<\/strong>\u8ba1\u7b97\u4f60\u81f3\u5c11\u9700\u8981\u62e5\u6709\u591a\u5c11\u73b0\u91d1\u624d\u80fd\u786e\u4fdd\u4f60\u80fd\u8d62\u5f97\u8fd9\u4e2a\u6e38\u620f\u3002<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6781\u5c0f\u5316\u6781\u5927\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\uff1a608 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8671.12% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8680.08% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\u3002 \u53c2\u8003\u4e86\u7f51\u7ad9\u63d0\u4f9b\u7684\u601d\u8def https:\/\/leetcode-cn.com\/problems\/guess-number-higher-or-lower-ii\/solution\/pythondong-tai-gui-hua-xiao-bai-ling-ji-chu-jiao-2\/\n\n\n        1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23\n    1   0   1   2   4   6   8   10  12  14  16  18  21  24  27  30  34  38  42  46  49  52  55  58\n    2       0   2   3   6   8\n    3           0   3   4   8\n    4               0   4   5\n    5                   0   5\n    6                       0\n\n    \u4f7f\u7528dp[i][j]\u8868\u793a\uff0c \u4ecei~j\u9700\u8981\u7684\u6700\u5c11\u91d1\u94b1\u3002\n        1. [i][i] = 0\n            \u4e00\u4e2a\u6570\u5b57\u65f6\uff0c\u4e0d\u9700\u8981\u731c\n        2. [i][i+1] = i\n            5~6\u65f6\uff0c\u731c5\n        3. \u5bf9\u4e8e[i][j]\n            \u901a\u8fc7 dp[i][j] = min(dp[i][x-1], dp[x+1][j]) + x .  for x in range(i, j)\n            \u4e5f\u5c31\u662f\uff0c\u5c06 i~j \u4ee5i~j\u4e2d\u7684\u67d0\u4e00\u6570\u5b57\u4e3a\u4e2d\u5fc3\u5207\u6210\u4e0d\u540c\u7684\u7247\u6bb5\uff0c\u53d6\u524d\u534a\u90e8\u5206\u5207\u7247\u4e0e\u540e\u534a\u90e8\u5206\u5207\u7247\u7684\u6700\u5927\u503c \u52a0\u4e0a\u8be5\u6570\u5b57 \u4f5c\u4e3a\u5f53\u524d\u5207\u7247\u7684\u91d1\u94b1\n            \u5c06\u4e0d\u540c\u5207\u7247\u60c5\u51b5\u7684\u6700\u5c0f\u503c\uff0c\u4f5c\u4e3a\u5f53\u524di~j\u7684\u6700\u7ec8\u91d1\u94b1\n\n\"\"\"\nclass Solution:\n    def getMoneyAmount(self, n: int) -&gt; int:\n        dp = [[0 for _ in range(n+1)] for _ in range(n+1)]\n        for i in range(1, n):\n            dp[i][i+1] = i  # dp[i][i+1] = 1\n        for j in range(1, n+1): # \u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u904d\u5386\u987a\u5e8f\u3002\n            for i in range(j-2, 0, -1): # \u5ffd\u7565[i][i] \u4e0e [i][i+1]\n                dp[i][j] = min(max(dp[i][x-1], dp[x+1][j])+x for x in range(i,j))   # \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\n        return dp[1][n]<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u6b63\u5728\u73a9\u4e00\u4e2a\u731c\u6570\u6e38\u620f\uff0c\u6e38\u620f\u89c4\u5219\u5982\u4e0b\uff1a \u6211\u4ece&nbsp;1&nbsp;\u5230 n \u4e4b\u95f4\u9009\u62e9\u4e00\u4e2a\u6570\u5b57\uff0c\u4f60\u6765\u731c\u6211\u9009\u4e86\u54ea&#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\/435"}],"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=435"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":436,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions\/436"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}