{"id":317970,"date":"2021-02-06T00:13:25","date_gmt":"2021-02-05T16:13:25","guid":{"rendered":"http:\/\/4563.org\/?p=317970"},"modified":"2021-02-06T00:13:25","modified_gmt":"2021-02-05T16:13:25","slug":"%e5%be%ae%e8%bd%af%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e6%b5%81%e6%b5%aa%e5%89%91%e5%ae%a2%e6%96%af%e6%b8%a9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=317970","title":{"rendered":"\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u6d41\u6d6a\u5251\u5ba2\u65af\u6e29"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u6d41\u6d6a\u5251\u5ba2\u65af\u6e29               <\/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<h3>\u63cf\u8ff0<\/h3>\n<p>\u5728\u7269\u8d28\u4f4d\u9762\u201c\u73b0\u5b9e\u201d\u4e2d\uff0c\u6709 n+1 \u4e2a\u661f\u7403\uff0c\u5206\u522b\u4e3a\u661f\u7403 0\uff0c\u661f\u7403 1\uff0c\u2026\u2026\uff0c\u661f\u7403 n \u3002<\/p>\n<p>\u6bcf\u4e00\u4e2a\u661f\u7403\u90fd\u6709\u4e00\u4e2a\u4f20\u9001\u95e8\uff0c\u901a\u8fc7\u4f20\u9001\u95e8\u53ef\u4ee5\u76f4\u63a5\u5230\u8fbe\u76ee\u6807\u661f\u7403\u800c\u4e0d\u7ecf\u8fc7\u5176\u4ed6\u7684\u661f\u7403\u3002<\/p>\n<p>\u4e0d\u8fc7\u4f20\u9001\u95e8\u6709\u4e24\u4e2a\u7f3a\u70b9\u3002<\/p>\n<p>\u7b2c\u4e00\uff0c\u4ece\u661f\u7403 i \u901a\u8fc7\u4f20\u9001\u95e8\u53ea\u80fd\u5230\u8fbe\u7f16\u53f7\u6bd4 i \u5927\uff0c\u4e14\u4e0e i \u7684\u5dee\u4e0d\u8d85\u8fc7 limit \u7684\u661f\u7403\u3002<\/p>\n<p>\u7b2c\u4e8c\uff0c\u901a\u8fc7\u4f20\u9001\u95e8\u5230\u8fbe\u661f\u7403 j\uff0c\u9700\u8981 cost[j]\u4e2a\u91d1\u5e01\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6d41\u6d6a\u5251\u5ba2\u65af\u6e29\u5230\u8fbe\u661f\u7403 0 \u540e\u8eab\u4e0a\u6709 m \u4e2a\u91d1\u5e01\uff0c\u8bf7\u95ee\u4ed6\u6709\u591a\u5c11\u79cd\u65b9\u6cd5\u901a\u8fc7\u4f20\u9001\u95e8\u5230\u8fbe\u661f\u7403 n \uff1f<\/p>\n<ul>\n<li>1 &lt;= n &lt;= 50, 0 &lt;= m &lt;= 100, 1 &lt;= limit &lt;= 50, 0 &lt;= cost[i] &lt;= 100 \u3002<\/li>\n<li>\u7531\u4e8e cost[0]\u6ca1\u6709\u610f\u4e49\uff0c\u9898\u76ee\u4fdd\u8bc1 cost[0] = 0 \u3002<\/li>\n<\/ul>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1<\/h3>\n<p>\u6bd4\u5982 n = 15, \u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff1a<\/p>\n<pre><code>\u8f93\u5165: n = 1 m = 1,  limit = 1 cost = [0, 1] \u8f93\u51fa: 1 \u89e3\u91ca: \u65b9\u6848 1\uff1a\u661f\u7403 0\u2192\u661f\u7403 1 <\/code><\/pre>\n<h3>\u6837\u4f8b 2<\/h3>\n<pre><code>\u8f93\u5165: n = 1 m = 1 limit = 1 cost = [0,2] \u8f93\u51fa: 0 \u89e3\u91ca: \u65e0\u5408\u6cd5\u65b9\u6848 <\/code><\/pre>\n<h3>\u7b97\u6cd5\uff1adp<\/h3>\n<p>\u6211\u4eec\u7528 dp[i][j]dp[i][j]\u4ee3\u8868\u4ece\u661f\u7403 00 \u51fa\u53d1\u5230\u8fbe\u661f\u7403 ii \u540e\u62e5\u6709 jj \u4e2a\u91d1\u5e01\u7684\u65b9\u6848\u6570\u3002<\/p>\n<ul>\n<li>\n<p>\u8bbe\u7f6e\u521d\u59cb\u72b6\u6001\u4e3a\u5728\u7b2c 0 \u53f7\u661f\u7403\uff0c\u6b64\u65f6\u62e5\u6709 m \u4e2a\u5e01\u3002dp[0][m]=1dp[0][m]=1 \u3002<\/p>\n<\/li>\n<li>\n<p>\u6211\u4eec\u8003\u8651 dp[i][j]dp[i][j]\u7684\u524d\u7ee7\u72b6\u6001\uff0c\u53ef\u4ee5\u53d1\u73b0\uff0c\u6240\u6709\u7f16\u53f7\u6bd4 i \u5c0f\uff0c\u4e14\u5dee\u5728 limit \u4e4b\u5185\u7684\u90fd\u80fd\u8f6c\u79fb\u8fc7\u6765\uff0c\u5e76\u4e14\u8f6c\u79fb\u8fc7\u7a0b\u6d88\u8017 cost[i]cost[i]\u7684\u5e01\uff0c\u6240\u4ee5\u5bf9\u5b83\u7684\u524d\u7ee7\u72b6\u6001\u7684\u65b9\u6848\u6570\u7d2f\u52a0\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u5217\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5982\u4e0b\u6240\u793a\uff0c<\/p>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210205_601d6eb41421a.png\" alt=\"\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u6d41\u6d6a\u5251\u5ba2\u65af\u6e29\" \/><\/p>\n<ul>\n<li>\u6700\u540e\u56e0\u4e3a\u8981\u6c42\u603b\u65b9\u6848\u6570\uff0c\u5bf9\u4e8e sven \u5728\u7b2c nn \u53f7\u661f\u7403\u7684\u6240\u6709\u5269\u4f59\u91d1\u5e01\u6570\u91cf\u6c42\u548c\u5373\u53ef\u3002\u7b54\u6848 <img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210205_601d6ec0b22d6.png\" alt=\"\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u6d41\u6d6a\u5251\u5ba2\u65af\u6e29\" \/><\/li>\n<\/ul>\n<h3>\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6 O(n\u2217m\u2217limit)O(n\u2217m\u2217limit)<\/li>\n<li> \u7a7a\u95f4\u590d\u6742\u5ea6 O(n\u2217m)O(n\u2217m)\n<ul>\n<li>\u5c31\u662f dpi \u6240\u6709\u7684\u72b6\u6001\u6570<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/**      * @param n: the max identifier of planet.      * @param m: gold coins that Sven has.      * @param limit: the max difference.      * @param cost: the number of gold coins that reaching the planet j through the portal costs.      * @return: return the number of ways he can reach the planet n through the portal.      *\/     public long getNumberOfWays(int n, int m, int limit, int[] cost) {         \/\/          long[][] dp = new long[n + 1][m + 1];         for (int i = 0; i &lt; m; i++) {             dp[0][i] = 0;         }         dp[0][m] = 1;         for (int i = 1; i &lt;= n; i++) {             for (int j = 0; j &lt;= m; j++) {                 dp[i][j] = 0;                 for (int t = Math.max(0, i - limit); t &lt;= i - 1; t++) {                     if (j + cost[i] &lt;= m) {                         dp[i][j] += dp[t][j + cost[i]];                     }                 }             }         }         long ans = 0;         for (int i = 0; i &lt;= m; i++) {             ans += dp[n][i];         }         return ans;     } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u8003<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5196909\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Fdoit <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6c38\u8fdc\u6ef4\u795e\uff0c\u6cf0\u7f57\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u6d41\u6d6a\u5251\u5ba2\u65af\u6e29 \u8cc7\u6df1\u5927\u4f6c&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\/317970"}],"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=317970"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317970\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317970"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}