Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cee2806

Browse files
add 1388
1 parent 1b88c99 commit cee2806

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1388|[Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1388.java) | |Hard|DP|
1112
|1387|[Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1387.java) | |Medium|Sort, Graph|
1213
|1386|[Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1386.java) | |Medium|Array, Greedy|
1314
|1385|[Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1385.java) | |Easy|Array|
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1388;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1388Test {
10+
private static _1388.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1388.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(10, solution1.maxSizeSlices(new int[]{1, 2, 3, 4, 5, 6}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)