forked from rituburman/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrodcutting_topdown.java
More file actions
68 lines (66 loc) · 947 Bytes
/
rodcutting_topdown.java
File metadata and controls
68 lines (66 loc) · 947 Bytes
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
class A
{
public int rod_cut(int p[],int n)
{
int r[]=new int[n];
for(int i=0;i<n;i++)
{
r[i]=Integer.MIN_VALUE;
}
return calculating(p,r,n);
}
public int calculating(int p[],int r[],int n)
{
int q;
if(n==0)
{
return 0;
}
if(r[n-1]>=0)
{
return r[n-1];
}
else
{
int i;
q=Integer.MIN_VALUE;
for(i=0;i<n;i++)
{
q=max(q,p[i]+calculating(p,r,n-i-1));
}
r[n-1]=q;
return q;
}
}
public int max(int a,int b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
}
class rodcutting_topdown
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=0;
System.out.println("Enter the size of rod");
n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the prices");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
A ob=new A();
int ans=ob.rod_cut(arr,n);
System.out.println(ans);
}
}