-
Notifications
You must be signed in to change notification settings - Fork 5
/
MaximizeCuts.java
50 lines (48 loc) · 1.62 KB
/
MaximizeCuts.java
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
/*https://practice.geeksforgeeks.org/problems/cutted-segments1642/1/*/
class Solution
{
public int maximizeCuts(int n, int x, int y, int z)
{
int[] table = new int[n+1];
/*
If i-x is 0 then we can add 1 to it
Otherwise if table[i-x] is non-zero then only we can add 1 to it
If table[i-x] is 0, that simply means we cannot cut that further
Same for y and z too
*/
for (int i = 1; i <= n; ++i)
{
int max = Integer.MIN_VALUE;
max = Math.max(max,(i-x == 0 || (i-x > 0 && table[i-x] > 0) ? table[i-x]+1 : 0));
max = Math.max(max,(i-y == 0 || (i-y > 0 && table[i-y] > 0) ? table[i-y]+1 : 0));
max = Math.max(max,(i-z == 0 || (i-z > 0 && table[i-z] > 0) ? table[i-z]+1 : 0));
table[i] = max;
}
return table[n];
}
}
class Solution
{
Integer[] store;
//Function to find the maximum number of cuts.
public int maximizeCuts(int n, int x, int y, int z)
{
//Your code here
store = new Integer[n+1];
store[n] = cut(n,x,y,z);
return store[n] == Integer.MIN_VALUE ? 0 : store[n];
}
public int cut(int n, int x, int y, int z)
{
if (n == 0) return 0;
if (store[n] != null) return store[n];
int result = 0;
if (n >= x) result = Math.max(result,cut(n-x,x,y,z)+1);
if (n >= y) result = Math.max(result,cut(n-y,x,y,z)+1);
if (n >= z) result = Math.max(result,cut(n-z,x,y,z)+1);
if (result == 0)
store[n] = Integer.MIN_VALUE;
else store[n] = result;
return store[n];
}
}