zoukankan      html  css  js  c++  java
  • [BinarySearch] Maximum Product Path in 2D Matrix

    You are given a two-dimensional list of integers matrix. You are currently at the top left corner and want to move to the bottom right corner. In each move, you can move down or right.

    Return the maximum product of the cells visited by going to the bottom right cell. If the result is negative, return -1. Otherwise, mod the result by 10 ** 9 + 7.

    Constraints

    • 1 ≤ n, m ≤ 20 where n and m are the number of rows and columns in matrix
    • -2 ≤ matrix[r][c] ≤ 2

    Example 1

    Input

    matrix = [
        [2, 1, -2],
        [-1, -1, -2],
        [1, 1, 1]
    ]

    Output

    8

    Explanation

    We can take the following path: [2, 1, -2, -2, 1].

    Dynamic programming:  This is essentially the 2D version of Maximum Subarray Product. We'll have 2 dp table: maxDp and minDp;  maxDp[i][j] is the max product path that ends at cell(i, j); minDp[i][j] is the min product path that ends at cell(i, j). The key here is that when matrix[i][j] is negative, we need to use the min of the previous 2 neighboring products to compute the max product; similiarly use the max of the previous 2 neighboring products to compute the min product at ends at cell(i, j).  The rest of the dp is pretty straightforward as shown in the following code.

    class Solution {
        public int solve(int[][] matrix) {
            int n = matrix.length, m = matrix[0].length, mod = (int)1e9 + 7;
            if(n == 0) return 0;
            long[][] maxDp = new long[n][m], minDp = new long[n][m];
            maxDp[0][0] = matrix[0][0];
            minDp[0][0] = matrix[0][0];
            for(int i = 1; i < n; i++) {
                maxDp[i][0] = maxDp[i - 1][0] * matrix[i][0];
                minDp[i][0] = minDp[i - 1][0] * matrix[i][0];
            }
            for(int j = 1; j < m; j++) {
                maxDp[0][j] = maxDp[0][j - 1] * matrix[0][j];
                minDp[0][j] = minDp[0][j - 1] * matrix[0][j];
            }
            for(int i = 1; i < n; i++) {
                for(int j = 1; j < m; j++) {
                    if(matrix[i][j] > 0) {
                        maxDp[i][j] = Math.max(maxDp[i - 1][j], maxDp[i][j - 1]) * matrix[i][j];     
                        minDp[i][j] = Math.min(minDp[i][j - 1], minDp[i - 1][j]) * matrix[i][j];
                    }
                    else if(matrix[i][j] < 0) {
                        maxDp[i][j] = Math.min(minDp[i - 1][j], minDp[i][j - 1]) * matrix[i][j];     
                        minDp[i][j] = Math.max(maxDp[i][j - 1], maxDp[i - 1][j]) * matrix[i][j];
                    }
                    else {
                        maxDp[i][j] = 0;
                        minDp[i][j] = 0;
                    }
                }
            }
            return maxDp[n - 1][m - 1] >= 0 ? (int)(maxDp[n - 1][m - 1] % mod) : -1;
        }
    }

    Related Problems

    Maximum Subarray Product

  • 相关阅读:
    Oracle 语法中的 INSERT INTO
    [Oracle]高效的SQL语句之分析函数(一)sum()
    Oracle:trunc()函数简介
    ORACLE 调试输出,字符串执行函数
    Oracle 的几种循环方式介绍
    js 判断字符串是否存在某个字符串
    IntelliJ IDEA 2021.3 旗舰版 官方中文正式版(附汉化包+安装教程)
    主线程中同步的 XMLHttpRequest 已不推荐使用,因其对终端用户的用户体验存在负面影响。可访问 http://xhr.spec.whatwg.org/ 详细了解
    js杂记:x:function(){}
    ORACLE 两表关联更新三种方式
  • 原文地址:https://www.cnblogs.com/lz87/p/14370645.html
Copyright © 2011-2022 走看看