-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax rope cutting .txt
52 lines (51 loc) · 1.08 KB
/
Max rope cutting .txt
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.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int T=Integer.parseInt(br.readLine());
while(T-->0)
{
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n+1];
for(int i=1;i<=n;i++)
{
arr[i]=i;
}
long[][] dp=new long[n+1][n];
for(long[] x:dp)
{
Arrays.fill(x,-1);
}
long ans=maxropecut(n,n-1,dp);
sb.append(ans+"\n");
}
System.out.print(sb);
}
public static long maxropecut(int n,int k,long[][] dp)
{
if(k==0)
{
return 0l;
}
if(n==0)
{
return 1l;
}
if(n<1)
{
return 0l;
}
if(dp[n][k]!=-1)
{
return dp[n][k];
}
long inc=k*maxropecut(n-k,k,dp);
long exc=maxropecut(n,k-1,dp);
return dp[n][k]=Math.max(inc,exc);
}
}