{"id":7202,"date":"2024-04-11T17:01:02","date_gmt":"2024-04-11T09:01:02","guid":{"rendered":""},"modified":"2024-04-11T17:01:02","modified_gmt":"2024-04-11T09:01:02","slug":"\u52a8\u6001\u89c4\u5212\u2014\u2014\u722c\u697c\u68af\u95ee\u9898\uff08\u722c\u697c\u68af+\u6700\u7701\u529b\u722c\u697c\u68af\uff09","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7202.html","title":{"rendered":"\u52a8\u6001\u89c4\u5212\u2014\u2014\u722c\u697c\u68af\u95ee\u9898\uff08\u722c\u697c\u68af+\u6700\u7701\u529b\u722c\u697c\u68af\uff09"},"content":{"rendered":"

1\u3001\u95ee\u9898<\/strong>\uff1a\u529b\u6263\u539f\u9898<\/p>\n

\"\u52a8\u6001\u89c4\u5212\u2014\u2014\u722c\u697c\u68af\u95ee\u9898\uff08\u722c\u697c\u68af+\u6700\u7701\u529b\u722c\u697c\u68af\uff09<\/p>\n<\/p>\n

2\u3001\u5206\u6790<\/strong><\/p>\n

\u6839\u636e\u9898\u610f\u5c55\u5f00\u5206\u6790\uff0c\u722c\u7b2c\u4e00\u5c42\u697c\u68af\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u722c\u5230\u7b2c\u4e8c\u5c42\u697c\u68af\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u90a3\u4e48\u722c\u5230\u7b2c\u4e09\u5c42\u697c\u68af\u53ef\u4ee5\u901a\u8fc7\u4ece\u7b2c\u4e00\u5c42\u697c\u68af\u4e00\u6b21\u6027\u8de8\u4e24\u6b65\u5230\u7b2c\u4e09\u5c42\u6216\u8005\u4ece\u7b2c\u4e8c\u5c42\u697c\u68af\u4e00\u6b21\u8de8\u4e00\u6b65\u5230\u7b2c\u4e09\u5c42\u3002\u6240\u4ee5\u5230\u7b2c\u4e09\u5c42\u697c\u68af\u7684\u72b6\u6001\u53ef\u4ee5\u7531\u7b2c\u4e00\u5c42\u697c\u68af\u548c\u7b2c\u4e8c\u5c42\u697c\u68af\u7684\u72b6\u6001\u8fdb\u884c\u63a8\u5bfc\uff0c\uff0c\u6545\u8be5\u9898\u53ef\u91c7\u7528dp\u6765\u5b9e\u73b0\u3002<\/p>\n

dp\u4e94\u90e8\u66f2\uff1a<\/p>\n

1) \u786e\u5b9adp\u6570\u7ec4\u53ca\u542b\u4e49<\/p>\n

dp[i]\u8868\u793a\u722c\u5230\u7b2ci\u5c42\u697c\u68af\u603b\u7684\u65b9\u6cd5\u6570<\/p>\n

2\uff09\u786e\u5b9a\u9012\u63a8\u516c\u5f0f<\/p>\n

        dp[i]= dp[i-1] + dp[i-2];  <\/p>\n

        dp[i-1]\u8868\u793a\u722c\u5230\u7b2ci-1\u5c42\u6709dp[i-1]\u79cd\u65b9\u6cd5\u53ef\u884c\uff0c\u90a3\u4e48\u518d\u5f80\u4e0a\u8d70\u4e00\u6b65\u5c31\u5230\u7b2ci\u5c42\u697c\u68af\uff1b<\/p>\n

         dp[i-2]\u8868\u793a\u722c\u5230\u7b2ci-2\u5c42\u6709dp[i-2]\u79cd\u65b9\u6cd5\u53ef\u884c\uff0c\u90a3\u4e48\u518d\u5f80\u4e0a\u8d702\u6b65\u5c31\u5230\u7b2ci\u5c42\u697c\u68af\uff1b<\/p>\n

3\uff09\u521d\u59cb\u5316dp<\/p>\n

        dp[0]=1;dp[1]=1;dp[2]=2;<\/p>\n

4)\u786e\u5b9a\u904d\u5386\u987a\u5e8f<\/p>\n

        \u4ece\u9012\u63a8\u516c\u5f0f\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u904d\u5386\u987a\u5e8f\u4e00\u5b9a\u662f\u4ece\u524d\u5411\u540e\u904d\u5386\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u524d\u9762\u7684\u77e5\u9053\u4e86 \u624d\u53ef\u4ee5\u8ba1\u7b97\u540e\u9762\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n

3\u3001\u4ee3\u7801\uff1a<\/strong><\/p>\n

class Solution {\n    public int climbStairs(int n) {\n        if(n<=1) return n;\n        int[] dp = new int[n+1];\n        dp[1]=1;\n        dp[2]=2;\n        for(int i=3;i<=n;i++){\n            dp[i]=dp[i-1]+dp[i-2];\n        }\n\n        return dp[n];\n    }\n}<\/code><\/pre>\n<\/p>\n

------------------------------------------------------------------------------------------------------------<\/strong><\/span><\/p>\n

\u95ee\u9898\u8fdb\u9636\uff1a\u6700\u5c0f\u82b1\u8d39\u722c\u697c\u68af<\/strong><\/span><\/p>\n

1\u3001\u9898\u76ee\uff1a\u529b\u6263\u539f\u9898<\/p>\n

\"\u52a8\u6001\u89c4\u5212\u2014\u2014\u722c\u697c\u68af\u95ee\u9898\uff08\u722c\u697c\u68af+\u6700\u7701\u529b\u722c\u697c\u68af\uff09<\/p>\n

 2\u3001\u9898\u76ee\u5206\u6790\uff1a<\/p>\n

\u6bcf\u5f53\u4f60\u722c\u4e0a\u4e00\u4e2a\u9636\u68af\u4f60\u90fd\u8981\u82b1\u8d39\u5bf9\u5e94\u7684\u4f53\u529b\u503c\uff0c\u4e00\u65e6\u652f\u4ed8\u4e86\u76f8\u5e94\u7684\u4f53\u529b\u503c\uff0c\u4f60\u5c31\u53ef\u4ee5\u9009\u62e9\u5411\u4e0a\u722c\u4e00\u4e2a\u9636\u68af\u6216\u8005\u722c\u4e24\u4e2a\u9636\u68af\uff0c\u6240\u4ee5\u793a\u4f8b1\u4e2d\u53ea\u82b1\u8d39\u4e00\u4e2a15 \u5c31\u53ef\u4ee5\u5230\u9636\u68af\u9876\uff0c\u6700\u540e\u4e00\u6b65\u53ef\u4ee5\u7406\u89e3\u4e3a \u4e0d\u7528\u82b1\u8d39\u3002<\/p>\n<\/p>\n

dp\u4e94\u90e8\u66f2\uff1a<\/p>\n

1\uff09\u786e\u5b9adp\u6570\u7ec4\u4ee5\u53ca\u4e0b\u6807\u7684\u542b\u4e49<\/p>\n

        dp[i]: \u5230\u8fbe\u7b2ci\u4e2a\u53f0\u9636\u6240\u82b1\u8d39\u7684\u6700\u5c11\u4f53\u529b\u4e3adp[i]**\u3002\uff08\u6ce8\u610f\u8fd9\u91cc\u8ba4\u4e3a\u662f\u7b2c\u4e00\u6b65\u4e00\u5b9a\u662f\u8981\u82b1\u8d39\uff09<\/p>\n

2)\u786e\u5b9a\u9012\u63a8\u516c\u5f0f\uff1a<\/p>\n

        \u53ef\u4ee5\u6709\u4e24\u4e2a\u9014\u5f84\u5f97\u5230dp[i]\uff0c\u4e00\u4e2a\u662fdp[i-1] \u4e00\u4e2a\u662fdp[i-2]\uff0c\u56e0\u4e3a\u5b58\u5728\u4f53\u529b\u82b1\u8d39\uff0c\u90a3\u4e48\u9009\u62e9\u4ecedp[i-1]\u8fd8\u662fdp[i-2]\u5230\u7b2ci\u5c42\u53f0\u9636\u5c3c\uff1f  \u4e3a\u4fdd\u8bc1\u82b1\u8d39\u6700\u5c11\uff0c\u6240\u4ee5\u5e94\u8be5\u9009\u62e9min(dp[i-1] ,dp[i-2] )+cost[i]<\/p>\n

>>\u6ce8\u610f<\/strong>\uff0c\u52a0cost[i]\uff0c\u800c\u4e0d\u662fcost[i-1],cost[i-2]\u4e4b\u7c7b\u7684**\uff0c\u56e0\u4e3a\u9898\u76ee\u4e2d\u8bf4\u4e86\uff1a\u6bcf\u5f53\u4f60\u722c\u4e0a\u4e00\u4e2a\u9636\u68af\u4f60\u90fd\u8981\u82b1\u8d39\u5bf9\u5e94\u7684\u4f53\u529b\u503c<\/p>\n

3\uff09dp\u6570\u7ec4\u521d\u59cb\u5316<\/p>\n

dp[0] = cost[0];\ndp[1] = cost[1];<\/code><\/pre>\n

4\uff09\u904d\u5386\u987a\u5e8f\uff1a<\/p>\n

   \u4ece\u524d\u5f80\u540e<\/p>\n<\/p>\n

3\u3001\u4ee3\u7801\uff1a<\/p>\n

class Solution {\n    public int minCostClimbingStairs(int[] cost) {\n        if(cost==null||cost.length==0){\n            return 0;\n        }\n        if(cost.length==1){\n            return cost[0];\n        }\n        int [] dp = new int[cost.length];\n        dp[0]=cost[0];\n        dp[1]=cost[1];\n        for(int i=2;i<cost.length;i++){\n            dp[i]=Math.min(dp[i-1],dp[i-2])+cost[i];\n        }\n        \/\/\u6700\u540e\u4e00\u6b65\uff0c\u5982\u679c\u662f\u4ece\u5012\u6570\u7b2c\u4e8c\u6b65\u722c\uff0c\u5219\u6700\u540e\u4e00\u6b65\u7684\u4f53\u529b\u82b1\u8d39\u53ef\u4ee5\u4e0d\u7528\u7b97\n        return Math.min(dp[cost.length-1],dp[cost.length-2]);\n\n\n          \n\n    }\n}<\/code><\/pre>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"\u52a8\u6001\u89c4\u5212\u2014\u2014\u722c\u697c\u68af\u95ee\u9898\uff08\u722c\u697c\u68af+\u6700\u7701\u529b\u722c\u697c\u68af\uff091\u3001\u95ee\u9898\uff1a\u529b\u6263\u539f\u98982\u3001\u5206\u6790\u6839\u636e\u9898\u610f\u5c55\u5f00\u5206\u6790\uff0c\u722c\u7b2c\u4e00\u5c42\u697c\u68af\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u722c\u5230\u7b2c\u4e8c...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7202"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7202"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7202\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}