forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
number-of-ways-of-cutting-a-pizza.cpp
39 lines (38 loc) · 1.59 KB
/
number-of-ways-of-cutting-a-pizza.cpp
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
// Time: O(m * n * k * (m + n))
// Space: O(m * n * k)
class Solution {
public:
int ways(vector<string>& pizza, int k) {
static int MOD = 1e9 + 7;
vector<vector<int>> prefix(pizza.size(), vector<int>(pizza[0].size()));
for (int j = pizza[0].size() - 1; j >= 0; --j) {
for (int accu = 0, i = pizza.size() - 1; i >= 0; --i) {
accu += pizza[i][j] == 'A';
prefix[i][j] = ((j + 1 != pizza[0].size()) ? prefix[i][j + 1] : 0) + accu;
}
}
vector<vector<vector<uint64_t>>> dp(pizza.size(),
vector<vector<uint64_t>>(pizza[0].size(),
vector<uint64_t>(k)));
for (int i = pizza.size() - 1; i >= 0; --i) {
for (int j = pizza[0].size() - 1; j >= 0; --j) {
dp[i][j][0] = 1;
for (int m = 1; m < k; ++m) {
for (int n = i + 1; n < pizza.size() && prefix[n][j]; ++n) {
if (prefix[i][j] == prefix[n][j]) {
continue;
}
dp[i][j][m] = (dp[i][j][m] + dp[n][j][m - 1]) % MOD;
}
for (int n = j + 1; n < pizza[0].size() && prefix[i][n]; ++n) {
if (prefix[i][j] == prefix[i][n]) {
continue;
}
dp[i][j][m] = (dp[i][j][m] + dp[i][n][m - 1]) % MOD;
}
}
}
}
return dp[0][0][k - 1];
}
};