Maximum Non Negative Product in a Matrix

You are given a rows x cols matrix grid. Initially, you are located at the top-left corner (0, 0), and in each step, you can only move right or down in the matrix.

Among all possible paths starting from the top-left corner (0, 0) and ending in the bottom-right corner (rows - 1, cols - 1), find the path with the maximum non-negative product. The product of a path is the product of all integers in the grid cells visited along the path.

Return the maximum non-negative product modulo 109 + 7If the maximum product is negative return -1.

Notice that the modulo is performed after getting the maximum product.

Example 1:

Input: grid = [[-1,-2,-3],
               [-2,-3,-3],
               [-3,-3,-2]]
Output: -1
Explanation: It's not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.

Example 2:

Input: grid = [[1,-2,1],
               [1,-2,1],
               [3,-4,1]]
Output: 8
Explanation: Maximum non-negative product is in bold (1 * 1 * -2 * -4 * 1 = 8).

思路:因为有正负数,所以三维dp,每个position有最大值和最小值,那么由上面的上面和左边的两个坐标来,就分两种情况;分别都要update 最大值和最小值;

class Solution {
    private double MOD = 1e9+7;
    public int maxProductPath(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        double[][][] dp = new double[n][m][2];
        dp[0][0][0] = grid[0][0]; //  max
        dp[0][0][1] = grid[0][0]; //  min;
        
        // init 1st col;
        for(int i = 1; i < n; i++) {
            dp[i][0][0] = (dp[i - 1][0][0] * grid[i][0]);
            dp[i][0][1] = (dp[i - 1][0][1] * grid[i][0]);
        }
        
        // init 1st row;
        for(int j = 1; j < m; j++) {
            dp[0][j][0] = (dp[0][j - 1][0]* grid[0][j]);
            dp[0][j][1] = (dp[0][j - 1][1]* grid[0][j]);
        }
        
        // calcualte matrix;
        for(int i = 1; i < n; i++) {
            for(int j = 1; j < m; j++) {
               if(grid[i][j] > 0) {
                   dp[i][j][0] = Math.max(dp[i - 1][j][0] * grid[i][j], dp[i][j - 1][0] * grid[i][j]);
                   dp[i][j][1] = Math.min(dp[i - 1][j][1] * grid[i][j], dp[i][j - 1][1] * grid[i][j]);
               } else if(grid[i][j] < 0) {
                   dp[i][j][0] = Math.max(dp[i - 1][j][1] * grid[i][j], dp[i][j - 1][1] * grid[i][j]); 
                   dp[i][j][1] = Math.min(dp[i - 1][j][0] * grid[i][j], dp[i][j - 1][0] * grid[i][j]);
               } else {
                   // grid[i][j] == 0;
                   dp[i][j][0] = 0;
                   dp[i][j][1] = 0;
               }
            }
        }
        double res = dp[n - 1][m - 1][0] % MOD;
        return res < 0 ? -1 : (int)res;
    }
}

这题也可以用dfs,因为只是往下走,所以不需要用cache

class Solution {
    private int MOD = 1000000007;
    public int maxProductPath(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        double[] res = new double[]{-1};
        dfs(grid, 0, 0, res, grid[0][0]);
        return res[0] < 0 ? -1 : (int)(res[0] % MOD);
    }
    
    private void dfs(int[][] grid, int x, int y, double[] res, double product) {
        if(x == grid.length - 1 && y == grid[0].length - 1) {
            res[0] = Math.max(res[0], product);
            return;
        }
        if(grid[x][y] == 0) {
            res[0] = Math.max(res[0], 0);
            return;
        }
        if(x + 1 < grid.length) {
            dfs(grid, x + 1, y, res, product * grid[x + 1][y]);
        }
        if(y + 1< grid[0].length) {
            dfs(grid, x, y + 1, res, product * grid[x][y + 1]);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值