[Lintcode] #700 杆子分割
来源:互联网 发布:数据恢复 mac osx 编辑:程序博客网 时间:2024/06/11 01:44
题目描述
给一个 n
英寸长的杆子和一个包含所有小于 n
的尺寸的价格. 确定通过切割杆并销售碎片可获得的最大值.例如,如果棒的长度为8,并且不同长度部件的值如下,则最大可获得值为 22
(通过切割两段长度 2
和 6
)
样例
长度 | 1 2 3 4 5 6 7 8 --------------------------------------------价格 | 1 5 8 9 10 17 17 20
给出 price = [1, 5, 8, 9, 10, 17, 17, 20]
, n = 8
返回 22
//切成长度为 2 和 6 的两段
长度 | 1 2 3 4 5 6 7 8 --------------------------------------------价格 | 3 5 8 9 10 17 17 20
给出 price = [3, 5, 8, 9, 10, 17, 17, 20]
, n = 8
返回 24
//切成长度为 1 的 8 段
public class Solution { /* * @param : the prices * @param : the length of rod * @return: the max value */ public int cutting(int[] prices, int n) { // Write your code here int[] dp = new int[n + 1];for (int i = 1; i <= n; ++i) {int cur = Integer.MIN_VALUE;for (int j = 1; j <= i / 2; ++j) {cur = Math.max(cur, dp[j] + dp[i - j]);}dp[i] = Math.max(cur, prices[i - 1]);}return dp[n]; }}
阅读全文
0 0
- [Lintcode] #700 杆子分割
- lintcode---杆子分割
- 杆子分割-LintCode
- LintCode- 分割回文串
- lintcode ----奇偶分割数组
- LintCode:分割回文串
- lintcode,奇偶分割数组
- 分割回文串-LintCode
- Lintcode奇偶分割数组
- 奇偶分割数组-LintCode
- LintCode 分割字符串
- 分割字符串-LintCode
- LintCode-分割回文串 II
- lintcode-奇偶分割数组-373
- lintcode-分割回文串-136
- 奇偶分割数组(LintCode)
- LintCode: 分割回文串 II
- lintcode(136)分割回文串
- 执行sql的时候为什么会出现Parameter index out of range (1 > number of parameters, which is 0)?
- 常用的混淆代码
- Cosos2d-x创建新的HelloWorld工程 Mac
- Hostapd
- test1
- [Lintcode] #700 杆子分割
- 阿里云总裁胡晓明:未来和合作伙伴一起_服务1000万家企业
- MyBatis
- 变量的作用域
- <IOS><Xcode>解决Can't map file, errno=22 file 'xxx' for architecture arm64(armv7)问题!
- Python基础小笔记《2017-10-17》
- JavaScript的对象,赋值,即用等号赋值如x=y,也想Java类一样(Java基本数据类型不是这样的)是满足“箭头”黄金定律,如果修改x,y的值也随之改变
- 测试用例设计方法
- Dockerfile中如何自动回答标准输入的问题