{"id":2681,"date":"2022-04-09T17:08:30","date_gmt":"2022-04-09T09:08:30","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2681"},"modified":"2022-04-09T17:08:30","modified_gmt":"2022-04-09T09:08:30","slug":"dp%e7%ac%ac13%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2681","title":{"rendered":"dp\u7b2c13\u5929"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/minimum-falling-path-sum\/\">931. \u4e0b\u964d\u8def\u5f84\u6700\u5c0f\u548c<\/a><\/p>\n\n\n\n<p>\u8fd9\u9898\u548c\u4e4b\u524d\u7684\u4e09\u89d2\u5f62\u4e0b\u964d\u6700\u5c0f\u548c\u4e00\u6837\uff0c\u8fb9\u754c\u5224\u65ad\u66f4\u9ebb\u70e6\u4e00\u70b9\u3002\u7528\u5230\u7684\u90fd\u662f\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5<\/p>\n\n\n\n<p><a href=\"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2634\">\u7b97\u6cd5\u5165\u95e8\u7b2c12\u5929 \u2013 zeroheart<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><em>\/**<br><\/em><em> 931. \u4e0b\u964d\u8def\u5f84\u6700\u5c0f\u548c<br><\/em><em> \u7ed9\u4f60\u4e00\u4e2a n x n \u7684 \u65b9\u5f62 \u6574\u6570\u6570\u7ec4 matrix \uff0c\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u901a\u8fc7 matrix \u7684\u4e0b\u964d\u8def\u5f84 \u7684 \u6700\u5c0f\u548c \u3002<br><\/em><em><br><\/em><em> \u4e0b\u964d\u8def\u5f84 \u53ef\u4ee5\u4ece\u7b2c\u4e00\u884c\u4e2d\u7684\u4efb\u4f55\u5143\u7d20\u5f00\u59cb\uff0c\u5e76\u4ece\u6bcf\u4e00\u884c\u4e2d\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u5728\u4e0b\u4e00\u884c\u9009\u62e9\u7684\u5143\u7d20\u548c\u5f53\u524d\u884c\u6240\u9009\u5143\u7d20\u6700\u591a\u76f8\u9694\u4e00\u5217\uff08\u5373\u4f4d\u4e8e\u6b63\u4e0b\u65b9\u6216\u8005\u6cbf\u5bf9\u89d2\u7ebf\u5411\u5de6\u6216\u8005\u5411\u53f3\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u3002<br><\/em><em> \u5177\u4f53\u6765\u8bf4\uff0c\u4f4d\u7f6e (row, col) \u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u5e94\u5f53\u662f (row + 1, col - 1)\u3001(row + 1, col) \u6216\u8005 (row + 1, col + 1) \u3002<br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1amatrix = [[2,1,3],[6,5,4],[7,8,9]]<br><\/em><em> \u8f93\u51fa\uff1a13<br><\/em><em> \u89e3\u91ca\uff1a\u5982\u56fe\u6240\u793a\uff0c\u4e3a\u548c\u6700\u5c0f\u7684\u4e24\u6761\u4e0b\u964d\u8def\u5f84<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1amatrix = [[-19,57],[-40,-5]]<br><\/em><em> \u8f93\u51fa\uff1a-59<br><\/em><em> \u89e3\u91ca\uff1a\u5982\u56fe\u6240\u793a\uff0c\u4e3a\u548c\u6700\u5c0f\u7684\u4e0b\u964d\u8def\u5f84<br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question931 {<br>    public static int minFallingPathSum(int[][] matrix) {<br>        \/\/ \u4e4b\u524d\u6709\u4e2a\u4e09\u89d2\u5f62\u6700\u5c0f\u8def\u5f84\uff0c\u8bd5\u4e0b\u662f\u4e0d\u662f\u4e00\u6837\u7684\u903b\u8f91\uff0c\u5b9e\u9645\u770b\u6765\u662f\u4e00\u6837\u7684\uff0c\u4e3b\u8981\u8981\u5904\u7406\u8fb9\u754c\u95ee\u9898<br>        int len = matrix.length;<br><br>        int[][] dp = new int[len+1][len+1];<br><br>        for(int i = len-1; i&gt;=0; i--){<br>            for(int j = 0; j&lt;len; j++){<br>                if(j==0) {<br>                    dp[i][j] = matrix[i][j] + Math.<em>min<\/em>(dp[i+1][j], dp[i+1][j+1]);<br>                } else if(j==len-1){<br>                    dp[i][j] = matrix[i][j] + Math.<em>min<\/em>(dp[i+1][j-1], dp[i+1][j]);<br>                }else {<br>                    dp[i][j] = matrix[i][j] + Math.<em>min<\/em>(dp[i+1][j-1], Math.<em>min<\/em>(dp[i+1][j], dp[i+1][j+1]));<br>                }<br>            }<br>        }<br><br>        int res = Integer.<em>MAX_VALUE<\/em>;<br>        for(int i = 0; i&lt;len; i++){<br>            res = Math.<em>min<\/em>(res, dp[0][i]);<br>        }<br><br>        return res;<br>    }<br><br>    public static void main(String[] args) {<br>\/\/        minFallingPathSum(new int[][]{{2,1,3},{6,5,4},{7,8,9}});<br>        <em>minFallingPathSum<\/em>(new int[][]{{82,81},{69,33}});<br>    }<br>}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>931. \u4e0b\u964d\u8def\u5f84\u6700\u5c0f\u548c \u8fd9\u9898\u548c\u4e4b\u524d\u7684\u4e09\u89d2\u5f62\u4e0b\u964d\u6700\u5c0f\u548c\u4e00\u6837\uff0c\u8fb9\u754c\u5224\u65ad\u66f4\u9ebb\u70e6\u4e00\u70b9\u3002\u7528\u5230\u7684\u90fd\u662f\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5 \u7b97\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[158],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2681"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2681"}],"version-history":[{"count":4,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2681\/revisions"}],"predecessor-version":[{"id":2685,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2681\/revisions\/2685"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}