|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * 1388. Pizza With 3n Slices |
| 7 | + * |
| 8 | + * There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: |
| 9 | + * You will pick any pizza slice. |
| 10 | + * Your friend Alice will pick next slice in anti clockwise direction of your pick. |
| 11 | + * Your friend Bob will pick next slice in clockwise direction of your pick. |
| 12 | + * Repeat until there are no more slices of pizzas. |
| 13 | + * Sizes of Pizza slices is represented by circular array slices in clockwise direction. |
| 14 | + * |
| 15 | + * Return the maximum possible sum of slice sizes which you can have. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: slices = [1,2,3,4,5,6] |
| 19 | + * Output: 10 |
| 20 | + * Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. |
| 21 | + * |
| 22 | + * Example 2: |
| 23 | + * Input: slices = [8,9,8,6,1,1] |
| 24 | + * Output: 16 |
| 25 | + * Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. |
| 26 | + * |
| 27 | + * Example 3: |
| 28 | + * Input: slices = [4,1,2,5,8,3,1,9,7] |
| 29 | + * Output: 21 |
| 30 | + * |
| 31 | + * Example 4: |
| 32 | + * Input: slices = [3,1,2] |
| 33 | + * Output: 3 |
| 34 | + * |
| 35 | + * Constraints: |
| 36 | + * 1 <= slices.length <= 500 |
| 37 | + * slices.length % 3 == 0 |
| 38 | + * 1 <= slices[i] <= 1000 |
| 39 | + * */ |
| 40 | +public class _1388 { |
| 41 | + public static class Solution1 { |
| 42 | + public int maxSizeSlices(int[] slices) { |
| 43 | + int n = slices.length; |
| 44 | + int[] b = Arrays.copyOf(slices, 2 * n); |
| 45 | + for (int i = 0; i < n; i++) { |
| 46 | + b[i + n] = slices[i]; |
| 47 | + } |
| 48 | + int[][] dp = new int[2 * n][2 * n]; |
| 49 | + for (int len = 3; len <= n; len += 3) { |
| 50 | + for (int i = 0; i + len - 1 < 2 * n; i++) { |
| 51 | + int j = i + len - 1; |
| 52 | + for (int k = i + 3; k <= j - 2; k += 3) { |
| 53 | + dp[i][j] = Math.max(dp[i][j], dp[i][k - 1] + dp[k][j]); |
| 54 | + } |
| 55 | + for (int k = i + 1; k < j; k += 3) { |
| 56 | + dp[i][j] = Math.max(dp[i][j], |
| 57 | + (i + 1 <= k - 1 ? dp[i + 1][k - 1] : 0) |
| 58 | + + b[k] + (k + 1 <= j - 1 ? dp[k + 1][j - 1] : 0) |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + int ans = 0; |
| 64 | + for (int i = 0; i < n; i++) { |
| 65 | + ans = Math.max(ans, dp[i][i + n - 1]); |
| 66 | + } |
| 67 | + return ans; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments