-
Notifications
You must be signed in to change notification settings - Fork 106
/
pizza-with-3n-slices.cpp
61 lines (56 loc) · 2.17 KB
/
pizza-with-3n-slices.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Time: O(n^2)
// Space: O(n)
// [observation]
// 1. we can never take two adjacent slices
// 2. if we want some set of N / 3 non-adjacent slices, there is always a way to take
//
// [proof]
// - for N = 3, it is obviously true.
// - for N' = N + 3,
// - because it's impossible to have only one unwanted slices between all wanted slices.
// if it's true, there will be 3N'/2 unwanted slices rather than 2N' unwanted ones, -><-
// - so we can always find a sequence of two unwanted slices with one wanted slice
// to take firstly, then we can find a way to take the remaining N ones by induction, QED
// better optimized space
class Solution {
public:
int maxSizeSlices(vector<int>& slices) {
return max(maxSizeSlicesLinear(slices, 0, slices.size() - 1),
maxSizeSlicesLinear(slices, 1, slices.size()));
}
private:
int maxSizeSlicesLinear(const vector<int>& slices, int start, int end) {
vector<vector<int>> dp(2, vector<int>(slices.size() / 3 + 1));
for (int i = start; i < end; ++i) {
for (int j = min(((i - start + 1) - 1) / 2 + 1, int(slices.size()) / 3);
j >= 1;
--j) {
dp[i % 2][j] = max(dp[(i - 1 + 2) % 2][j],
dp[(i - 2 + 2) % 2][j - 1] + slices[i]);
}
}
return dp[(end - 1) % 2][slices.size() / 3];
}
};
// Time: O(n^2)
// Space: O(n)
class Solution2 {
public:
int maxSizeSlices(vector<int>& slices) {
return max(maxSizeSlicesLinear(slices, 0, slices.size() - 1),
maxSizeSlicesLinear(slices, 1, slices.size()));
}
private:
int maxSizeSlicesLinear(const vector<int>& slices, int start, int end) {
vector<vector<int>> dp(3, vector<int>(slices.size() / 3 + 1));
for (int i = start; i < end; ++i) {
for (int j = 1;
j <= min(((i - start + 1) - 1) / 2 + 1, int(slices.size()) / 3);
++j) {
dp[i % 3][j] = max(dp[(i - 1 + 3) % 3][j],
dp[(i - 2 + 3) % 3][j - 1] + slices[i]);
}
}
return dp[(end - 1) % 3][slices.size() / 3];
}
};