博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(HW)rod cutting(Java)
阅读量:6738 次
发布时间:2019-06-25

本文共 1299 字,大约阅读时间需要 4 分钟。

1 public class test 2 { 3     public static void main(String[] args) 4     { 5         Scanner input = new Scanner(System.in); 6         int n = input.nextInt(); 7         //第一个位置不使用 8         int[] prices = new int[] {0,1,5,8,9,10,17,17,20,24,30}; 9         int[] r = new int[n + 1];10         for(int i = 0; i < r.length; i++)11             r[i] = Integer.MIN_VALUE;12         System.out.println(MaxProfit(n, prices, r));13     }    14     15     //recursion16     public static int MaxProfit(int n, int[] prices, int[] r)17     {18         if(n == 0)19             return 0;20         21         int tmpMax = Integer.MIN_VALUE;22         for(int i = 1; i <= n; i++)23         {24             //避免重复计算25             if(r[n - i] == Integer.MIN_VALUE)26                 r[n - i] = MaxProfit(n - i, prices, r);27             tmpMax = Math.max(tmpMax, prices[i] + r[n - i]);28         }29         30         return tmpMax;31     }32     33     //iteration 时间复杂度: O(N^2)34     public static int MaxProfit(int n, int[] prices, int[] r)35     {36         r[0] = 0;37         for(int i = 1; i <= n; i++)38             for(int j = 1; j <= i; j++)39                 r[i] = Math.max(r[i], prices[j] + r[i - j]);40         41         return r[n];42     }43 }

 

转载于:https://www.cnblogs.com/Huayra/p/10957266.html

你可能感兴趣的文章
戴尔虚拟集成系统(VIS)的总体经济影响
查看>>
关于AbstractList
查看>>
linux安装telnet服务器
查看>>
python核心编程:学习笔记3--迭代器,列表解析
查看>>
linux 默认目录介绍
查看>>
网站建设应遵循用户需求胜于一切
查看>>
Archivelog和Noarcivelog
查看>>
8月第一周全球各国家域名总量统计:美国持续领先
查看>>
IDC评述网:《2013年度香港域名注册总量报告》
查看>>
Python进程缓存
查看>>
基于PCDN技术的无延时直播方案
查看>>
阿里云移动端播放器高级功能---直播时移
查看>>
javascript string 转unicode unicode 转string
查看>>
opencv ubuntu golang
查看>>
From Apprentice To Artisan 翻译 20
查看>>
我的友情链接
查看>>
Nagios和Centreon的安装部署
查看>>
shel脚本--监控网卡
查看>>
集中管理服务器 PUPPET(一) 部署 与 添加节点
查看>>
rhca 333 network
查看>>