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 ...
随机推荐
- (备忘)怎么去除WinRAR弹窗广告?
1.在WinRAR的安装目录下新建一个记事本,命名为“rarreg.key”. 2.打开记事本,将一下内容复制进去. RAR registration data Federal Agency for ...
- Spring MVC原理及配置详解
Spring MVC概述: Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制 ...
- ORA-39006错误原因及解决办法
使用impdp导出数据时碰到ora-39006错误,错误提示如下所示: ORA-39006: internal error ORA-39213: Metadata processing is not ...
- reactjs中使用高德地图计算两个经纬度之间的距离
第一步下载依赖 npm install --save react-amap 第二步,在组件中使用 import React, { Component } from 'react' import { L ...
- Java IO--NIO(一)
一.基本概念描述 1.1 I/O简介 I/O即输入输出,是计算机与外界世界的一个接口.IO操作的实际主题是操作系统.在java编程中,一般使用流的方式来处理IO,所有的IO都被视作是单个字节的移动,通 ...
- DB2 错误代码
sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 +098 0 ...
- myeclipse集成meavn
环境准备: JDK 1.6 Maven 3.0.4 myeclipse 8.6.1 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版 ...
- 各种数据库连接字符串 -- c#
sqlite : connectionString="Data Source=|DataDirectory|\databasename.db;Pooling=true;FailIfMissi ...
- Windows下应用级别的IIS负载均衡方案 Application Request Route
转载于:https://blog.csdn.net/2000killer/article/details/51228625 序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一 ...
- WDTP注册破解
简介 WDTP 不止是一款开源免费的 GUI 桌面单机版静态网站生成器和简单方便的前端开发工具,更是一款跨平台的集笔记.个人知识管理.写作/创作.博客/网站内容与样式管理等功能于一体的多合一内容处理/ ...