python_lintcode_700Cutting a Rod_418整数转罗马数字
来源:互联网 发布:网络用语野生什么意思 编辑:程序博客网 时间:2024/06/05 04:19
700Cutting a Rod
题目
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
您在真实的面试中是否遇到过这个题? Yes
样例
length | 1 2 3 4 5 6 7 8 --------------------------------------------price | 1 5 8 9 10 17 17 20Given price = [1, 5, 8, 9, 10, 17, 17, 20], n = 8Return 22 // by cutting in two pieces of lengths 2 and 6length | 1 2 3 4 5 6 7 8 --------------------------------------------price | 3 5 8 9 10 17 17 20Given price = [3, 5, 8, 9, 10, 17, 17, 20], n = 8Return 24 // by cutting in eight pieces of length 1
思路
- 题目:给定一个长度为n,然后将其切杆和销售,根据长度和价值表,得到最大的价值。
length | 1 2 3 4 5 6 7 8 --------------------------------------------price | 1 5 8 9 10 17 17 20Given price = [1, 5, 8, 9, 10, 17, 17, 20], n = 8Return 22 // by cutting in two pieces of lengths 2 and 6例如,如果杆的长度n=8,获得的最大价值是22(通过削减两块长度2和6--》5+17=22)
length | 1 2 3 4 5 6 7 8 --------------------------------------------price | 3 5 8 9 10 17 17 20Given price = [3, 5, 8, 9, 10, 17, 17, 20], n = 8Return 24 // by cutting in eight pieces of length 1如果n = 8,则通过消减成8块,长度皆为1,则价值3*8=24
- 杆可以切为多个某长度的杆,这是完全背包问题:完全背包问题每种物品可以装入无限次,将长度看做背包的容量,长度为物品的体重,不同长度的杆的价值为物品的价值。
- 用两个循环去找到切杆为j后的最大价值,result[j]表示已切杆长j后的价值,即result[j]=max(result[j],result[j-i]+prices[i])
- 推荐博客:http://www.jianshu.com/p/7a4e6071bc02
代码
class Solution: """ @param: : the prices @param: : the length of rod @return: the max value """ def cutting(self, prices, n): # Write your code here #完全背包问题 #存放每一长的最大价值,从1---->n,初始化为0 result = list( 0 for x in range(n+1) ) prices = [0] + prices #i为每个价值下的长度 for i in range(1,len(prices)): for j in range(1,n+1): if i <= j: result[j]= max(result[j],result[j-i]+prices[i]) return result[-1]
418整数转罗马数字
题目
给定一个整数,将其转换成罗马数字。
返回的结果要求在1-3999的范围内。
样例
4 -> IV
12 -> XII
21 -> XXI
99 -> XCIX
思路
- 这道题必须要去了解罗马数字
https://baike.so.com/doc/5330839-5566013.html - 罗马数字:大 小=大+小还有小 大=大-小。
- 所以直接将整数按个十百千位,去找罗马数字,然后再合并。
代码
class Solution: """ @param: n: The integer @return: Roman representation """ def intToRoman(self, n): # write your code here #个位数 ge = [0,'I','II','III','IV','V','VI','VII','VIII','IX'] #十位数 shi = [0,'X','XX','XXX','XL','L','LX','LXX','LXXX','XC'] #百位数 bai = [0,'C','CC','CCC','CD','D','DC','DCC','DCCC','CM'] #千位 qian= [0,'M','MM','MMM'] #result存结果 result = '' if n >=1000: result = result + qian[n//1000] n = n % 1000 if 100 <= n <1000: result = result + bai[n//100] n = n % 100 if 10 <= n <100: result = result + shi[n//10] n = n % 10 if 0 < n <10: result = result + ge[n] return result
阅读全文
0 0
- python_lintcode_700Cutting a Rod_418整数转罗马数字
- LeetCode 罗马数字转整数
- 罗马数字转整数
- 整数转罗马数字
- LintCode:整数转罗马数字
- LintCode_419_罗马数字转整数
- LintCode_418_整数转罗马数字
- Lintcode 罗马数字转整数
- [Lintcode]罗马数字转整数
- lintcode -- 罗马数字转整数
- lintcode -- 整数转罗马数字
- Lingcode 整数转罗马数字
- 罗马数字转整数
- 整数转罗马数字-LintCode
- 罗马数字转整数-LintCode
- lintcode-罗马数字转整数-419
- Integer to Roman 整数转罗马数字
- [LeetCode]Integer to Roman整数转罗马数字
- Ubuntu 10.10下安装与配置Squid代理软件
- 看完一个在校大学生的Java学习历程,我觉得我还能学得更多
- mybatis从入门到放弃(1)
- texturePacker的使用
- lua笔记(1)
- python_lintcode_700Cutting a Rod_418整数转罗马数字
- 模型的定义
- WPF
- iOS 根据时间戳获取星期几
- PHP_SCREW 加密
- iOS 根据视频url获取某一帧图片并实现三级缓存
- vs各种目录库的配置总结
- Simulator 定位设置 TouchID/FaceID模拟
- BZOJ1096 [ZJOI2007]仓库建设