-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPizzaTime.java
More file actions
52 lines (41 loc) · 1.5 KB
/
PizzaTime.java
File metadata and controls
52 lines (41 loc) · 1.5 KB
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
import java.util.*;
public class PizzaTime {
private static HashMap memo = new HashMap();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
System.out.println(solve(n));
}
sc.close();
}
private static int solve(int n) {
memo.clear();
return maxSlicesHaoCanEat(n);
}
private static int maxSlicesHaoCanEat(int slices) {
// Base cases
if (slices <= 2) {
return 0; // Alex eats all remaining slices
}
if (memo.containsKey(String.valueOf(slices))) {
return (Integer) memo.get(String.valueOf(slices));
}
int maxHaoSlices = 0;
// Try all valid partitions (m1, m2, m3)
// where m1 + m2 + m3 = slices and 1 ≤ m1 ≤ m2 ≤ m3
for (int m1 = 1; m1 <= slices / 3; m1++) {
for (int m2 = m1; m2 <= (slices - m1) / 2; m2++) {
int m3 = slices - m1 - m2;
if (m2 <= m3) {
// Hao eats m1 slices today + optimal from remaining m3 slices
int totalHaoSlices = m1 + maxSlicesHaoCanEat(m3);
maxHaoSlices = Math.max(maxHaoSlices, totalHaoSlices);
}
}
}
memo.put(String.valueOf(slices), maxHaoSlices);
return maxHaoSlices;
}
}