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 ...
随机推荐
- day5_不能循环删除list-深拷贝、浅拷贝(import copy)
一.循环删list里面的元素,会导致下标错位,结果是不对的举例:想删除奇数 l = [1,1,1,2,3,4,5] for i in l: if i%2 !=0: l.remove(i) #删除后,导 ...
- phpstorm 破解版
原链接https://blog.csdn.net/gu_wen_jie/article/details/79136475
- Asynchronous Programming
https://msdn.microsoft.com/zh-cn/library/dd997423.aspx http://www.cnblogs.com/luminji/archive/2010/0 ...
- 024-母版页MasterPage
网站的布局通常是统一的,上面是Logo.菜单条.下面是公司地址.版权声明等.如果每个页面都重复做这些功能的话:重复性劳动.一旦修改那么每个页面都要修改..Net中一般用母版(MasterPage)技术 ...
- Sitecore中Core,Master和Web数据库之间的区别
Core数据库 正如名称所示,Core Database是Sitecore应用程序的主干,它可用于多种用途. 核心数据库包含所有Sitecore设置. 它包含桌面模式,内容编辑器,页面编辑器等的定义. ...
- 洛谷P2820 局域网 (最小生成树)
题目链接:https://www.luogu.org/problemnew/show/P2820 题目背景 某个局域网内有n(n<=100)台计算机,由于搭建局域网时工作人员的疏忽,现在局域网内 ...
- 复习-css列表和表格相关属性
css列表和表格相关属性 list-style:设置所有列表属性 list-style-image:将图像设置为列表项标记,主要有url值 list-style-position:设置列表项标记的放置 ...
- SpringMVC登录拦截DEMO
交给/login的post请求的控制器处理, 并通过控制器的逻辑控制获取提示信息login.jsp<%-- Created by IntelliJ IDEA. User: shijinglu D ...
- Heroku 云服务部署流程
部署操作: heroic create bluefocus mkdir heroku && cd heroku --------------------- git init herok ...
- oracle 11.2 asynch descriptor resize等待事件
asynch descriptor resize描述最近部分insert /*+ append */语句出现该等待时间,经查This event is set when the number of a ...