1 Star 0 Fork 0

蔡茂昌 / Algorithms-implemented-in-Java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
RodCutting.java 720 Bytes
一键复制 编辑 原始数据 按行查看 历史
/* A Dynamic Programming solution for Rod cutting problem
Returns the best obtainable price for a rod of
length n and price[] as prices of different pieces */
public class RodCutting {
private static int cutRod(int price[],int n)
{
int val[] = new int[n+1];
val[0] = 0;
for (int i = 1; i<=n; i++)
{
int max_val = Integer.MIN_VALUE;
for (int j = 0; j < i; j++)
max_val = Math.max(max_val,price[j] + val[i-j-1]);
val[i] = max_val;
}
return val[n];
}
//main function to test
public static void main(String args[])
{
int arr[] = new int[] {2, 5, 13, 19, 20};
int size = arr.length;
System.out.println("Maximum Obtainable Value is " +
cutRod(arr, size));
}
}
Java
1
https://gitee.com/edmondcmc/Algorithms-implemented-in-Java.git
git@gitee.com:edmondcmc/Algorithms-implemented-in-Java.git
edmondcmc
Algorithms-implemented-in-Java
Algorithms-implemented-in-Java
master

搜索帮助