-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path1594-maximum-non-negative-product-in-a-matrix.js
71 lines (63 loc) · 2.09 KB
/
1594-maximum-non-negative-product-in-a-matrix.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* 1594. Maximum Non Negative Product in a Matrix
* https://leetcode.com/problems/maximum-non-negative-product-in-a-matrix/
* Difficulty: Medium
*
* You are given a m x n 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 (m - 1, n - 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.
*/
/**
* @param {number[][]} grid
* @return {number}
*/
var maxProductPath = function(grid) {
const rows = grid.length;
const cols = grid[0].length;
const dp = Array.from({ length: rows }, () =>
Array.from({ length: cols }, () => [1, 1])
);
dp[0][0] = [grid[0][0], grid[0][0]];
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
if (row === 0 && col === 0) continue;
let minProduct = Infinity;
let maxProduct = -Infinity;
if (row > 0) {
minProduct = Math.min(
minProduct,
dp[row - 1][col][0] * grid[row][col],
dp[row - 1][col][1] * grid[row][col]
);
maxProduct = Math.max(
maxProduct,
dp[row - 1][col][0] * grid[row][col],
dp[row - 1][col][1] * grid[row][col]
);
}
if (col > 0) {
minProduct = Math.min(
minProduct,
dp[row][col - 1][0] * grid[row][col],
dp[row][col - 1][1] * grid[row][col]
);
maxProduct = Math.max(
maxProduct,
dp[row][col - 1][0] * grid[row][col],
dp[row][col - 1][1] * grid[row][col]
);
}
dp[row][col] = [minProduct, maxProduct];
}
}
const maxResult = dp[rows - 1][cols - 1][1];
return maxResult < 0 ? -1 : maxResult % (10 ** 9 + 7);
};