GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/MaximumNonNegativeProduct.java at master · LeBW/leetcode

Expand file tree

/

MaximumNonNegativeProduct.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
34 lines (34 loc) · 1.26 KB
/

MaximumNonNegativeProduct.java

Copy path

File metadata and controls

34 lines (34 loc) · 1.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 5521. Maximum Non Negative Product in a Matrix
* 动态规划。
* dp[i][j][0]:到[i, j]时的最小值
* dp[i][j][1]:到[i, j]时的最大值.
* @author LBW
*/
public class MaximumNonNegativeProduct {
public int maxProductPath(int[][] grid) {
int m = grid.length, n = grid[0].length;
long[][][] dp = new long[m][n][2];
dp[0][0][0] = dp[0][0][1] = grid[0][0];
// init
for (int i = 1; i < m; i++) {
dp[i][0][0] = dp[i][0][1] = dp[i-1][0][0] * grid[i][0];
}
for (int j = 1; j < n; j++) {
dp[0][j][0] = dp[0][j][1] = dp[0][j-1][0] * grid[0][j];
}
// fill the array
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j][0] = Math.min(Math.min(dp[i-1][j][0] * grid[i][j], dp[i-1][j][1] * grid[i][j]), Math.min(dp[i][j-1][0] * grid[i][j], dp[i][j-1][1] * grid[i][j]));
dp[i][j][1] = Math.max(Math.max(dp[i-1][j][0] * grid[i][j], dp[i-1][j][1] * grid[i][j]), Math.max(dp[i][j-1][0] * grid[i][j], dp[i][j-1][1] * grid[i][j]));
}
}
if (dp[m-1][n-1][1] >= 0) {
return (int)(dp[m-1][n-1][1] % (long)(1e9 + 7));
}
else {
return -1;
}
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.