{"id":7405,"date":"2020-09-21T00:39:43","date_gmt":"2020-09-21T07:39:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7405"},"modified":"2020-09-21T09:07:44","modified_gmt":"2020-09-21T16:07:44","slug":"leetcode-1594-maximum-non-negative-product-in-a-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1594-maximum-non-negative-product-in-a-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1594. Maximum Non Negative Product in a Matrix"},"content":{"rendered":"\n<p>You are given a&nbsp;<code>rows x cols<\/code>&nbsp;matrix&nbsp;<code>grid<\/code>.&nbsp;Initially, you&nbsp;are located at the top-left&nbsp;corner&nbsp;<code>(0, 0)<\/code>,&nbsp;and in each step, you can only&nbsp;<strong>move right&nbsp;or&nbsp;down<\/strong>&nbsp;in the matrix.<\/p>\n\n\n\n<p>Among all possible paths starting from the top-left corner&nbsp;<code>(0, 0)<\/code>&nbsp;and ending in the bottom-right corner&nbsp;<code>(rows - 1, cols - 1)<\/code>, find the path with the&nbsp;<strong>maximum non-negative product<\/strong>. The product of a path is the product of all integers in the grid cells visited along the path.<\/p>\n\n\n\n<p>Return the&nbsp;<em>maximum non-negative product&nbsp;<strong>modulo<\/strong>&nbsp;<\/em><code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.&nbsp;<em>If the maximum product is&nbsp;<strong>negative<\/strong>&nbsp;return&nbsp;<\/em><code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Notice that the modulo is performed after getting the maximum product.<\/strong><\/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> grid = [[-1,-2,-3],\n&nbsp;              [-2,-3,-3],\n&nbsp;              [-3,-3,-2]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> It's not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -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> grid = [[<strong>1<\/strong>,-2,1],\n&nbsp;              [<strong>1<\/strong>,<strong>-2<\/strong>,1],\n&nbsp;              [3,<strong>-4<\/strong>,<strong>1<\/strong>]]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> Maximum non-negative product is in bold (1 * 1 * -2 * -4 * 1 = 8).\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> grid = [[<strong>1<\/strong>, 3],\n&nbsp;              [<strong>0<\/strong>,<strong>-4<\/strong>]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Maximum non-negative product is in bold (1 * 0 * -4 = 0).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[ <strong>1<\/strong>, 4,4,0],\n&nbsp;              [<strong>-2<\/strong>, 0,0,1],\n&nbsp;              [ <strong>1<\/strong>,<strong>-1<\/strong>,<strong>1<\/strong>,<strong>1<\/strong>]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Maximum non-negative product is in bold (1 * -2 * 1 * -1 * 1 * 1 = 2).\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= rows, cols &lt;= 15<\/code><\/li><li><code>-4 &lt;= grid[i][j] &lt;= 4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Use two dp arrays,<\/p>\n\n\n\n<p>dp_max[i][j] := max product of matrix[0~i][0~j]<br>dp_min[i][j] := min product of matrix[0~i][0~j]<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m*n)<\/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  int maxProductPath(vector<vector<int>>& grid) {\n    constexpr int kMod = 1e9 + 7;\n    const int m = grid.size();\n    const int n = grid[0].size();\n    vector<vector<long>> dp_max(m, vector<long>(n));\n    vector<vector<long>> dp_min(m, vector<long>(n));\n    dp_max[0][0] = dp_min[0][0] = grid[0][0];\n    for (int i = 1; i < m; ++i)\n      dp_max[i][0] = dp_min[i][0] = dp_min[i - 1][0] * grid[i][0];\n    for (int j = 1; j < n; ++j)\n      dp_max[0][j] = dp_min[0][j] = dp_min[0][j - 1] * grid[0][j];\n    for (int i = 1; i < m; ++i)\n      for (int j = 1; j < n; ++j) {        \n        if (grid[i][j] >= 0) {\n          dp_max[i][j] = max(dp_max[i - 1][j], dp_max[i][j - 1]) * grid[i][j];\n          dp_min[i][j] = min(dp_min[i - 1][j], dp_min[i][j - 1]) * grid[i][j];\n        } else {\n          dp_max[i][j] = min(dp_min[i - 1][j], dp_min[i][j - 1]) * grid[i][j];\n          dp_min[i][j] = max(dp_max[i - 1][j], dp_max[i][j - 1]) * grid[i][j];\n        }\n      }\n    return dp_max[m - 1][n - 1] >= 0 ? dp_max[m - 1][n - 1] % kMod : -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;rows x cols&nbsp;matrix&nbsp;grid.&nbsp;Initially, you&nbsp;are located at the top-left&nbsp;corner&nbsp;(0, 0),&nbsp;and in each step, you can only&nbsp;move right&nbsp;or&nbsp;down&nbsp;in the matrix. Among all possible paths&#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":[18,216,177],"class_list":["post-7405","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-matrix","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7405","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=7405"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7405\/revisions"}],"predecessor-version":[{"id":7407,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7405\/revisions\/7407"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}