{"id":6001,"date":"2019-12-28T10:20:16","date_gmt":"2019-12-28T18:20:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6001"},"modified":"2019-12-29T01:53:33","modified_gmt":"2019-12-29T09:53:33","slug":"leetcode-1301-number-of-paths-with-max-score","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1301-number-of-paths-with-max-score\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1301. Number of Paths with Max Score"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1301. Number of Paths with Max Score - \u5237\u9898\u627e\u5de5\u4f5c EP289\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/WwdjLkWmDPs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given a square&nbsp;<code>board<\/code>&nbsp;of characters. You can move on the board starting at the bottom right square marked with the character&nbsp;<code>'S'<\/code>.<\/p>\n\n\n\n<p>You need&nbsp;to reach the top left square marked with the character&nbsp;<code>'E'<\/code>. The rest of the squares are labeled either with a numeric character&nbsp;<code>1, 2, ..., 9<\/code>&nbsp;or with an obstacle&nbsp;<code>'X'<\/code>. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.<\/p>\n\n\n\n<p>Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum,&nbsp;<strong>taken modulo&nbsp;<code>10^9 + 7<\/code><\/strong>.<\/p>\n\n\n\n<p>In case there is no path, return&nbsp;<code>[0, 0]<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> board = [\"E23\",\"2X2\",\"12S\"]\n<strong>Output:<\/strong> [7,1]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> board = [\"E12\",\"1X1\",\"21S\"]\n<strong>Output:<\/strong> [4,2]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> board = [\"E11\",\"XXX\",\"11S\"]\n<strong>Output:<\/strong> [0,0]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= board.length == board[i].length &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1301-ep299.png\" alt=\"\" class=\"wp-image-6007\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1301-ep299.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1301-ep299-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1301-ep299-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i][j] := max score when reach (j, i)<br>count[i][j] := path to reach (j, i) with max score<br><br>m = max(dp[i + 1][j], dp[i][j+1], dp[i+1][j+1])<br>dp[i][j] = board[i][j] + m<br>count[i][j] += count[i+1][j] if dp[i+1][j] == m<br>count[i][j] += count[i][j+1] if dp[i][j+1] == m<br>count[i][j] += count[i+1][j+1] if dp[i+1][j+1] == m<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n^2)<br><\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> pathsWithMaxScore(vector<string>& board) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = board.size();\n    vector<vector<int>> dp(n + 1, vector<int>(n + 1));\n    vector<vector<int>> cc(n + 1, vector<int>(n + 1, 0));\n    board[n - 1][n - 1] = board[0][0] = '0';\n    cc[n - 1][n - 1] = 1;\n    for (int i = n - 1; i >= 0; --i)\n      for (int j = n - 1; j >= 0; --j) {\n        if (board[i][j] != 'X') {          \n          int m = max({dp[i + 1][j], dp[i][j + 1], dp[i + 1][j + 1]});\n          dp[i][j] = (board[i][j] - '0') + m;\n          if (dp[i + 1][j] == m) \n            cc[i][j] = (cc[i][j] + cc[i + 1][j]) % kMod;\n          if (dp[i][j + 1] == m)\n            cc[i][j] = (cc[i][j] + cc[i][j + 1]) % kMod;\n          if (dp[i + 1][j + 1] == m)\n            cc[i][j] = (cc[i][j] + cc[i + 1][j + 1]) % kMod;\n        }\n      }\n    return {cc[0][0] ? dp[0][0] : 0, cc[0][0]};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a square&nbsp;board&nbsp;of characters. You can move on the board starting at the bottom right square marked with the character&nbsp;&#8216;S&#8217;. You need&nbsp;to reach&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[7,18,217,498],"class_list":["post-6001","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-board","tag-dp","tag-hard","tag-on2","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6001"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6001\/revisions"}],"predecessor-version":[{"id":6008,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6001\/revisions\/6008"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}