[LeetCode] Pizza With 3n Slices

1388. Pizza With 3n Slices

There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows:

  • You will pick any pizza slice.
  • Your friend Alice will pick the next slice in the anti-clockwise direction of your pick.
  • Your friend Bob will pick the next slice in the clockwise direction of your pick.
  • Repeat until there are no more slices of pizzas.

Given an integer array slices that represent the sizes of the pizza slices in a clockwise direction, return the maximum possible sum of slice sizes that you can pick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
int helper(vector<int> A, int m) {
int res = 0, n = A.size();
vector<vector<int>> dp(n + 2, vector<int>(m + 1));
for(int i = 2; i < n + 2; i++) {
for(int j = 1; j <= m; j++) {
// use max for eat(i - 1)th pizza as jth eat (i - 2)th pizza as jth with max for skip 2 pizza with (j - 1)th
dp[i][j] = max(dp[i-1][j], dp[i-2][j-1] + A[i-2]);
res = max(res, dp[i][j]);
}
}

return res;
}
public:
int maxSizeSlices(vector<int>& slices) {
int n = slices.size();
return max(
helper(vector<int>(slices.begin(), slices.begin() + n - 1), n / 3),
helper(vector<int>(slices.begin() + 1, slices.begin() + n), n / 3)
);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/01/PS/LeetCode/pizza-with-3n-slices/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.