553. Optimal Division
题目:
Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.
However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the maximum result, and return the corresponding expression in string format. Your expression should NOT contain redundant parenthesis.
Example:
Input: [1000,100,10,2]
Output: "1000/(100/10/2)"
Explanation:
1000/(100/10/2) = 1000/((100/10)/2) = 200
However, the bold parenthesis in "1000/((100/10)/2)" are redundant,
since they don't influence the operation priority. So you should return "1000/(100/10/2)". Other cases:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
Note:
- The length of the input array is [1, 10].
- Elements in the given array will be in range [2, 1000].
- There is only one optimal division for each test case.
思路:
存在这样一个基本事实:X1/X2/X3/……/Xn = X1/X2*Y,也就是说对于X1/X2/X3/……/Xn,无论怎样加括号,总是可以表示成X1/X2*Y的形式。若要使得X1/X2/X3/……/Xn的值最大,则Y值应该最大。当Y=X3*x4……*Xn时,可以得到最大值。
当Y为最大值时,X1/X2*Y = X1*X3*X4……*Xn/X2 = X1/(X2/X3/X4……/Xn)。
代码:
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
if len(nums) == :
return str(nums[])
elif len(nums) == :
return str(nums[])+'/'+str(nums[])
else:
result=""
for i in range(len(nums)-):
result += str(nums[i])+'/'
if i == :
result += '('
result += str(nums[len(nums)-])+')'
return result
参考:
http://www.cnblogs.com/hellowooorld/p/6807513.html
553. Optimal Division的更多相关文章
- LC 553. Optimal Division
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- 【LeetCode】553. Optimal Division 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】553. Optimal Division
题目如下: 解题思路:这是数学上的一个定理.对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN). 代码如下: class Solution(obj ...
- [LeetCode] Optimal Division 最优分隔
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- [Swift]LeetCode553. 最优除法 | Optimal Division
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- LeetCode Optimal Division
原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- LeetCode Weekly Contest 28
1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocode ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 分析servlet injection
@WebServlet("/cdiservlet") ||url映射 public class NewServlet extends HttpServlet { private M ...
- IDEA为了使用方便,需要改的几条配置
自动编译开关 在Eclipse中自动编译开关是开着的,如下所示那么,在IDEA中,务必要手动将其打开,非常重要! 忽略大小写开关 IDEA默认是匹配大小写,此开关如果未关.你输入字符一定要符合大小写. ...
- CentOS 7 服务端口表
# Note that it is presently the policy of IANA to assign a single well-known# port number for both T ...
- Zookeeper应用之——栅栏(barrier)
Zookeeper应用之——栅栏(barrier) 栅栏(barrier)简介 barrier的作用是所有的线程等待,知道某一时刻,锁释放,所有的线程同时执行.举一个生动的例子,比如跑步比赛,所有 运 ...
- Spring 工具包
Spring 工具包: spring-core-.RELEASE.jar!\org\springframework\util
- app埋点
目前APP埋点的主流有两种方式: 第一类是预先设定好想要获取的目标数据,让程序员撰写代码把“采集器”埋到相应的页面上,用于追踪和记录的用户的行为,并把实时数据传送到后台数据库或者客户端. 第二类方法是 ...
- nodejs笔记之初识node
1.安装node; node -v //检测node是否安装成功 node可以做什么: 搭建服务器: 读写文件: 连接数据库: 爬虫: node的模块系统: 原生模块(如http,fs); 自定义模 ...
- WSDL文档结构图
- MVC设计思路
MVC 学会重复.学会总结.学会预习和练习 前端页面 <----> 服务器(控制层.业务层.DAO层) <---> DB 说明:无论是框架还是servletJSP,用的 ...
- jquery简易tab切换
切换tab 使用eq()函数 eq() 方法将匹配元素集缩减值指定 index 上的一个. //为项目 3 设置红色背景 <ul> <li>list item 1</li ...