[leetcode] 1594. Maximum Non Negative Product in a Matrix

Description

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 + 7. If 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).

Example 3:

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

Example 4:

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

Constraints:

  • 1 <= rows, cols <= 15
  • -4 <= grid[i][j] <= 4

分析

题目的意思是:求矩阵从左上角到右下角最大非负乘积,如果为负数,则返回-1.这道题显然需要用动态规划,由于有负数,所以需要用两个dp数组,dp_max记录当前乘积的最大值,dp_min记录乘积的最小值。然后写一个双循环遍历更新dp_max和dp_min就行了。

dp_max[i][j]=max(dp_max[i-1][j]*grid[i][j],dp_max[i][j-1]*grid[i][j])
dp_max[i][j]=max(dp_max[i][j],dp_min[i-1][j]*grid[i][j],dp_min[i][j-1]*grid[i][j]) 
dp_min[i][j]=min(dp_max[i-1][j]*grid[i][j],dp_max[i][j-1]*grid[i][j])
dp_min[i][j]=min(dp_min[i][j],dp_min[i-1][j]*grid[i][j],dp_min[i][j-1]*grid[i][j])

代码

class Solution:
    def maxProductPath(self, grid: List[List[int]]) -> int:
        row=len(grid)
        col=len(grid[0])
        dp_max=[[0]*col for i in range(row)]
        dp_min=[[0]*col for i in range(row)]
        dp_max[0][0]=dp_min[0][0]=grid[0][0]
        for i in range(1,row):
            dp_max[i][0]=dp_max[i-1][0]*grid[i][0]
            dp_min[i][0]=dp_min[i-1][0]*grid[i][0]
            
        for i in range(1,col):
            dp_max[0][i]=dp_max[0][i-1]*grid[0][i]
            dp_min[0][i]=dp_min[0][i-1]*grid[0][i]
            
        for i in range(1,row):
            for j in range(1,col):
                dp_max[i][j]=max(dp_max[i-1][j]*grid[i][j],dp_max[i][j-1]*grid[i][j])
                dp_max[i][j]=max(dp_max[i][j],dp_min[i-1][j]*grid[i][j],dp_min[i][j-1]*grid[i][j]) 
                dp_min[i][j]=min(dp_max[i-1][j]*grid[i][j],dp_max[i][j-1]*grid[i][j])
                dp_min[i][j]=min(dp_min[i][j],dp_min[i-1][j]*grid[i][j],dp_min[i][j-1]*grid[i][j])
        if(dp_max[row-1][col-1]>=0):
            return dp_max[row-1][col-1]%(10**9+7)
        return -1

参考文献

1594 矩阵的最大非负积(动态规划)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值