2 Star 2 Fork 0

隋宏伟/algorithm

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
GetBestGoldMining.java 813 Bytes
一键复制 编辑 原始数据 按行查看 历史
隋宏伟 提交于 2019-08-01 16:05 +08:00 . feat: 动态规划求解金矿问题
class GetBestGoldMining {
/**
*
* @param w 工人总数
* @param p 每个金矿需要工人
* @param g 每个金矿的总收益
* @return 得到金子的最优解
*/
public static int getBestGoldMining(int w, int[] p, int[] g) {
int[] results = new int[w + 1];
for (int i = 1; i <= g.length; i++) {
for (int j = w; j >= 1; j--) {
if (j >= p[i - 1]) {
results[j] = Math.max(results[j], results[j - p[i - 1]] + g[i - 1]);
}
}
}
return results[w];
}
public static void main(String[] args) {
System.out.println("最优解为:"
+ getBestGoldMining(10, new int[] { 5, 5, 3, 4, 3 }, new int[] { 400, 500, 200, 300, 350 }) + "kg");
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/suihw/algorithm.git
git@gitee.com:suihw/algorithm.git
suihw
algorithm
algorithm
master

搜索帮助