{"id":419607,"date":"2021-03-31T11:03:45","date_gmt":"2021-03-31T03:03:45","guid":{"rendered":"http:\/\/4563.org\/?p=419607"},"modified":"2021-03-31T11:03:45","modified_gmt":"2021-03-31T03:03:45","slug":"%e7%94%a8%e6%9c%80%e5%b0%8f%e5%a0%86%e5%92%8c-dp-%e5%81%9a%e4%b8%80%e9%81%93%e8%85%be%e8%ae%af%e9%9d%a2%e8%af%95%e7%9c%9f%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=419607","title":{"rendered":"\u7528\u6700\u5c0f\u5806\u548c DP \u505a\u4e00\u9053\u817e\u8baf\u9762\u8bd5\u771f\u9898~"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7528\u6700\u5c0f\u5806\u548c DP \u505a\u4e00\u9053\u817e\u8baf\u9762\u8bd5\u771f\u9898~               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u539f\u9898\u76ee\u5728\u8fd9\u91cc\uff0c\u6211\u61d2\u5f97\u590d\u5236\u4e86\u76f4\u63a5\u653e\u6211\u81ea\u5df1\u5f97\u89e3\u6cd5\u5566~<\/p>\n<p>\u539f\u9898\u6233\u6211~<\/p>\n<h2>\u89e3\u9898\u601d\u8def 1\uff1a\u6700\u5c0f\u5806<\/h2>\n<ul>\n<li>\n<p>\u5f88\u5bb9\u6613\u60f3\u5230\u7684\u65b9\u6cd5\u662f\uff1a\u4ece 1 \u8d77\u68c0\u9a8c\u6bcf\u4e2a\u6570\u662f\u5426\u4e3a\u4e11\u6570\uff0c\u76f4\u5230\u627e\u5230 n \u4e2a\u4e11\u6570\u4e3a\u6b62\u3002\u4f46\u662f\u968f\u7740 n \u7684\u589e\u5927\uff0c\u7edd\u5927\u90e8\u5206\u6570\u5b57\u90fd\u4e0d\u662f\u4e11\u6570\uff0c\u6211\u4eec\u679a\u4e3e\u7684\u6548\u7387\u975e\u5e38\u4f4e\u3002\u56e0\u6b64\uff0c\u6362\u4e2a\u89d2\u5ea6\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u53ea\u751f\u6210\u4e11\u6570\uff0c\u4e14\u751f\u6210 n \u4e2a\uff0c\u8fd9\u9053\u9898\u5c31\u8fce\u5203\u800c\u89e3\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u96be\u53d1\u73b0\u751f\u6210\u4e11\u6570\u7684\u89c4\u5f8b\uff1a\u5982\u679c\u5df2\u77e5\u4e11\u6570 ugly\uff0c\u90a3\u4e48 ugly * 2\uff0cugly * 3 \u548c ugly * 5 \u4e5f\u90fd\u662f\u4e11\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u65e2\u7136\u6c42\u7b2c n \u5c0f\u7684\u4e11\u6570\uff0c\u53ef\u4ee5\u91c7\u7528\u6700\u5c0f\u5806\u6765\u89e3\u51b3\u3002\u6bcf\u6b21\u5f39\u51fa\u5806\u4e2d\u6700\u5c0f\u7684\u4e11\u6570\uff0c\u7136\u540e\u68c0\u67e5\u5b83\u5206\u522b\u4e58\u4ee5 2 \u30013 \u548c 5 \u540e\u7684\u6570\u662f\u5426\u751f\u6210\u8fc7\uff0c\u5982\u679c\u662f\u7b2c\u4e00\u6b21\u751f\u6210\uff0c\u90a3\u4e48\u5c31\u653e\u5165\u5806\u4e2d\u3002\u7b2c n \u4e2a\u5f39\u51fa\u7684\u6570\u5373\u4e3a\u7b2c n \u5c0f\u7684\u4e11\u6570\u3002 \u7b97\u6cd5\u6d41\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6700\u5c0f\u5806 heap\uff0c\u54c8\u5e0c\u8868 seen \u548c\u8d28\u56e0\u6570\u5217\u8868 factors = [2, 3, 5]\u3002heap \u7528\u4e8e\u5b58\u50a8\u5df2\u751f\u6210\u7684\u4e11\u6570\uff0c\u5f39\u51fa\u6700\u5c0f\u503c\uff1b seen \u7528\u4e8e\u6807\u8bb0\u5806\u4e2d\u51fa\u73b0\u8fc7\u7684\u5143\u7d20\uff0c\u907f\u514d\u91cd\u590d\u5165\u5806\u3002<\/p>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316\u5c06 1 \u5165\u5806\uff0c\u5e76\u6dfb\u52a0\u5230 seen \u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u590d\u4ee5\u4e0b\u6b65\u9aa4 n \u6b21\uff1a \u5f39\u51fa\u5806\u4e2d\u6700\u5c0f\u7684\u6570\u5b57 curr_ugly \u3002 \u5bf9\u4e8e factors \u4e2d\u6bcf\u4e2a\u56e0\u6570 f\uff0c\u751f\u6210\u65b0\u7684\u4e11\u6570 new_ugly \u3002\u82e5 new_ugly \u4e0d\u5728 seen \u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230 heap \u4e2d\u5e76\u66f4\u65b0 seen \u3002<\/p>\n<\/li>\n<li>\n<p>curr_ugly \u5373\u4e3a\u7b2c n \u5c0f\u7684\u4e11\u6570\uff0c\u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<ul>\n<li>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(nlog(n))O(nlog(n))\u3002\u5f39\u51fa\u6bcf\u4e2a\u6700\u5c0f\u503c\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a\u5806\u7684\u9ad8\u5ea6\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(nlog(n))O(nlog(n))\u3002<\/p>\n<\/li>\n<li>\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)O(n)\u3002\u904d\u5386 n \u4e2a\u4e11\u6570\uff0c\u5e76\u5c06\u6bcf\u4e2a\u4e11\u6570\u4e58\u4ee5 2 \u30013 \u548c 5 \u7684\u7ed3\u679c\u5b58\u5165\u5806\u4e2d\u3002\u5806\u548c\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u4e2a\u6570\u90fd\u4e0d\u4f1a\u8d85\u8fc7 n * 3 \u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u4ee3\u7801<\/h3>\n<pre><code>import heapq  class Solution:     \"\"\"     @param n: An integer     @return: return a  integer as description.     \"\"\"     def nthUglyNumber(self, n):         heap = []         heapq.heappush(heap, 1)          seen = set()         seen.add(1)          factors = [2, 3, 5]          curr_ugly = 1                  for _ in range(n):             # \u6bcf\u6b21\u5f39\u51fa\u5f53\u524d\u6700\u5c0f\u4e11\u6570             curr_ugly = heapq.heappop(heap)             # \u751f\u6210\u65b0\u7684\u4e11\u6570             for f in factors:                 new_ugly = curr_ugly * f                 if new_ugly not in seen:                     seen.add(new_ugly)                     heapq.heappush(heap, new_ugly)         return curr_ugly <\/code><\/pre>\n<h2>\u89e3\u9898\u601d\u8def 2\uff1a\u52a8\u6001\u89c4\u5212<\/h2>\n<ul>\n<li>\n<p>\u5728\u6700\u5c0f\u5806\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u7684\u601d\u8def\u662f\u628a\u5f53\u524d\u4e11\u6570\u80fd\u751f\u6210\u7684\u4e11\u6570\u90fd\u52a0\u5165\u5230\u5806\u4e2d\uff0c\u7136\u540e\u518d\u5f39\u51fa\u6700\u5c0f\u503c\u3002\u5982\u679c\u6211\u4eec\u80fd\u77e5\u9053\u4e0b\u4e00\u4e2a\u6700\u5c0f\u7684\u4e11\u6570\uff0c\u6bcf\u6b21\u53ea\u751f\u6210\u6700\u5c0f\u7684\u90a3\u4e2a\uff0c\u5c31\u53ef\u4ee5\u8282\u7701\u6700\u5c0f\u503c\u67e5\u8be2\u7684\u65f6\u95f4\u6d88\u8017\u3002<\/p>\n<\/li>\n<li>\n<p>\u91c7\u7528\u52a8\u6001\u89c4\u5212\u7684\u65b9\u6cd5\uff0c\u7528\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4 dp \u8bb0\u5f55\u524d n \u4e2a\u4e11\u6570\u3002\u4e09\u4e2a\u6307\u9488 l2\uff0cl3 \u548c l5 \u6307\u5411 dp \u4e2d\u7684\u5143\u7d20\uff0c\u6700\u5c0f\u7684\u4e11\u6570\u53ea\u53ef\u80fd\u51fa\u73b0\u5728 dp[l2]\u7684 2 \u500d\u3001dp[l3]\u7684 3 \u500d\u548c dp[l5]\u7684 5 \u500d\u4e09\u8005\u4e2d\u95f4\u3002\u901a\u8fc7\u79fb\u52a8\u4e09\u4e2a\u6307\u9488\uff0c\u5c31\u80fd\u4fdd\u8bc1\u751f\u6210\u7684\u4e11\u6570\u662f\u6709\u5e8f\u7684\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u7b97\u6cd5\u6d41\u7a0b<\/h3>\n<ul>\n<li>\n<p>\u521d\u59cb\u5316\u6570\u7ec4 dp \u548c\u4e09\u4e2a\u6307\u9488 l2 \u3001l3 \u548c l5 \u3002dp[0] = 1\uff0c\u8868\u793a\u6700\u5c0f\u7684\u4e11\u6570\u4e3a 1 \u3002\u4e09\u4e2a\u6307\u9488\u90fd\u6307\u5411 dp[0]\u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u590d\u4ee5\u4e0b\u6b65\u9aa4 n \u6b21\uff0cdp[i]\u8868\u793a\u7b2c i + 1 \u5c0f\u7684\u4e11\u6570\uff1a<\/p>\n<ul>\n<li>\n<p>\u6bd4\u8f83 2 * dp[l2], 3 * dp[l3], 5 * dp[l5]\u4e09\u8005\u5927\u5c0f\uff0c\u4ee4 dp[i]\u4e3a\u5176\u4e2d\u7684\u6700\u5c0f\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c dp[i] == 2 * dp[l2]\uff0cl2 \u6307\u9488\u540e\u79fb\u4e00\u4f4d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c dp[i] == 3 * dp[l3]\uff0cl3 \u6307\u9488\u540e\u79fb\u4e00\u4f4d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c dp[i] == 2 * dp[l5]\uff0cl5 \u6307\u9488\u540e\u79fb\u4e00\u4f4d\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>dp[n &#8211; 1]\u5373\u4e3a\u7b2c n \u5c0f\u7684\u4e11\u6570\uff0c\u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)O(n)\u3002\u751f\u6210\u4e00\u4e2a\u4e11\u6570\u53ea\u9700\u5e38\u91cf\u65f6\u95f4\uff0c\u6240\u4ee5\u751f\u6210 n \u4e2a\u4e11\u6570\u9700\u8981 O(n)O(n)\u3002<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)O(n)\u3002dp \u6570\u7ec4\u7684\u957f\u5ea6\u4e3a n \u3002<\/li>\n<\/ul>\n<h3>\u4ee3\u7801<\/h3>\n<pre><code>import heapq  class Solution:     \"\"\"     @param n: An integer     @return: return a  integer as description.     \"\"\"     def nthUglyNumber(self, n):         dp = [0] * n         dp[0] = 1         l2, l3, l5 = 0, 0, 0         for i in range(1, n):             # \u751f\u6210\u7b2c i+1 \u5c0f\u7684\u4e11\u6570             dp[i] = min(2 * dp[l2], 3 * dp[l3], 5 * dp[l5])             if dp[i] == 2 * dp[l2]:                 l2 += 1             if dp[i] == 3 * dp[l3]:                 l3 += 1             if dp[i] == 5 * dp[l5]:                 l5 += 1         return dp[n - 1] <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7528\u6700\u5c0f\u5806\u548c DP \u505a\u4e00\u9053\u817e\u8baf\u9762\u8bd5\u771f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419607"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419607"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419607\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=419607"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=419607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}