-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (152 loc) · 4.87 KB
/
main.cpp
File metadata and controls
170 lines (152 loc) · 4.87 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Source: https://leetcode.com/problems/maximum-non-negative-product-in-a-matrix
// Title: Maximum Non Negative Product in a Matrix
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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** `10^9 + 7`. If the maximum product is **negative**, return `-1`.
//
// Notice that the modulo is performed after getting the maximum product.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2021/12/23/product1.jpg
//
// ```
// Input: grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
// Output: -1
// Explanation: It is not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.
// ```
//
// **Example 2:**
// https://assets.leetcode.com/uploads/2021/12/23/product2.jpg
//
// ```
// Input: grid = [[1,-2,1],[1,-2,1],[3,-4,1]]
// Output: 8
// Explanation: Maximum non-negative product is shown (1 * 1 * -2 * -4 * 1 = 8).
// ```
//
// **Example 3:**
// https://assets.leetcode.com/uploads/2021/12/23/product3.jpg
//
// ```
// Input: grid = [[1,3],[0,-4]]
// Output: 0
// Explanation: Maximum non-negative product is shown (1 * 0 * -4 = 0).
// ```
//
// **Constraints:**
//
// - `m == grid.length`
// - `n == grid[i].length`
// - `1 <= m, n <= 15`
// - `-4 <= grid[i][j] <= 4`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <cstdint>
#include <vector>
using namespace std;
// 2D-DP
//
// Max[i][j] is the maximum product to (i, j)
// Min[i][j] is the minimum product to (i, j)
class Solution {
constexpr static int64_t modulo = 1e9 + 7;
public:
int maxProductPath(const vector<vector<int>>& grid) {
const int m = grid.size(), n = grid[0].size();
// Init DP
auto maxDp = vector<vector<int64_t>>(m, vector<int64_t>(n));
auto minDp = vector<vector<int64_t>>(m, vector<int64_t>(n));
maxDp[0][0] = grid[0][0];
minDp[0][0] = grid[0][0];
// i = 0
maxDp[0][0] = grid[0][0];
minDp[0][0] = grid[0][0];
for (int j = 1; j < n; ++j) {
auto [minProd, maxProd] = minmax({
maxDp[0][j - 1] * grid[0][j],
minDp[0][j - 1] * grid[0][j],
});
maxDp[0][j] = maxProd;
minDp[0][j] = minProd;
}
// i > 0
for (int i = 1; i < m; ++i) {
// j = 0
auto [minProd, maxProd] = minmax({
maxDp[i - 1][0] * grid[i][0],
minDp[i - 1][0] * grid[i][0],
});
maxDp[i][0] = maxProd;
minDp[i][0] = minProd;
// j > 0
for (int j = 1; j < n; ++j) {
auto [minProd, maxProd] = minmax({
maxDp[i][j - 1] * grid[i][j],
maxDp[i - 1][j] * grid[i][j],
minDp[i][j - 1] * grid[i][j],
minDp[i - 1][j] * grid[i][j],
});
maxDp[i][j] = maxProd;
minDp[i][j] = minProd;
}
}
return max(static_cast<int>(maxDp[m - 1][n - 1] % modulo), -1);
}
};
// 1D-DP
//
// Max[i][j] is the maximum product to (i, j)
// Min[i][j] is the minimum product to (i, j)
class Solution2 {
constexpr static int64_t modulo = 1e9 + 7;
public:
int maxProductPath(const vector<vector<int>>& grid) {
const int m = grid.size(), n = grid[0].size();
// Init DP
auto maxPrev = vector<int64_t>(n);
auto maxCurr = vector<int64_t>(n);
auto minPrev = vector<int64_t>(n);
auto minCurr = vector<int64_t>(n);
// i = 0
maxCurr[0] = grid[0][0];
minCurr[0] = grid[0][0];
for (int j = 1; j < n; ++j) {
auto [minProd, maxProd] = minmax({
maxCurr[j - 1] * grid[0][j],
minCurr[j - 1] * grid[0][j],
});
maxCurr[j] = maxProd;
minCurr[j] = minProd;
}
// i > 0
for (int i = 1; i < m; ++i) {
swap(maxCurr, maxPrev);
swap(minCurr, minPrev);
// j = 0
auto [minProd, maxProd] = minmax({
maxPrev[0] * grid[i][0],
minPrev[0] * grid[i][0],
});
maxCurr[0] = maxProd;
minCurr[0] = minProd;
// j > 0
for (int j = 1; j < n; ++j) {
auto [minProd, maxProd] = minmax({
maxCurr[j - 1] * grid[i][j],
maxPrev[j] * grid[i][j],
minCurr[j - 1] * grid[i][j],
minPrev[j] * grid[i][j],
});
maxCurr[j] = maxProd;
minCurr[j] = minProd;
}
}
return max(static_cast<int>(maxCurr[n - 1] % modulo), -1);
}
};