[leetcode-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 will always be equal to (X1/X2) * Y,no matter how you place parentheses.
i.e no matter how you place parentheses, X1 always goes to the numerator and X2 always goes to the denominator.
Hence you just need to maximize Y. And Y is maximized when it is equal to X3 *..*Xn.
So the answer is always X1/(X2/X3/../Xn) = (X1 *X3 *..*Xn)/X2”
有了这个结果其实就简单了。。。重要的是过程,要想得到最大结果,那么第一个数字X1一定是作为分子,第二个数X2一定是作为分母。
于是就有了X1/(X2/X3/../Xn) 。
string optimalDivision(vector<int>& nums)
{
string ret;
ret = to_string(nums[]);
if (nums.size() == )return ret;
if (nums.size() == ) return ret + "/" + to_string(nums[]);
ret += "/(" + to_string(nums[]);
for (int i = ; i < nums.size();i++)
{
ret += "/" + to_string(nums[i]);
}
ret += ")";
return ret;
}
参考:
https://discuss.leetcode.com/topic/86483/easy-to-understand-simple-o-n-solution-with-explanation/2
[leetcode-553-Optimal Division]的更多相关文章
- 【LeetCode】553. Optimal Division 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 553. Optimal Division
题目: Given a list of positive integers, the adjacent integers will perform the float division. For ex ...
- LC 553. Optimal Division
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- 【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 ...
- LeetCode Optimal Division
原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...
- [Swift]LeetCode553. 最优除法 | Optimal Division
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- [Leetcode Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- [LeetCode] 465. Optimal Account Balancing 最优账户平衡
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...
随机推荐
- c++ 自动应用类型转换
c++中,在赋值时如果类型不匹配,就会应用到:类型转换.类型转换又分为隐式转换(implicit conversion) 和 显式强制类型转换(emplcit conversion).在这我围绕着类的 ...
- swfit - 实现类似今日头条顶部标签和底部内容的动态解决方案
TYPageView TYPageView 类似今日头条 的标签导航解决方案,支持多种样式选择,基于swift3.0,支持文字颜色动态变化,底部选中线的动态变化 配图: 使用方法: let title ...
- 计蒜客蓝桥杯模拟赛五J. 程序设计:放置守卫
在一张 n 行 m 列的方格地图上放置一些守卫,每个守卫能守护上.左.右三个方向上相邻的方格和自己所在的方格.如下图,红色的方格放置守卫,绿色的方格为该守卫守护的区域. 现在要求在地图上放置若干个守卫 ...
- 【JAVAWEB学习笔记】28_jquery加强:json数据结构、jquery的ajax操作和表单校验插件
Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用 一.json数据结构 1.什么是json JSON(Jav ...
- javaWeb学习总结(10)- Filter(过滤器)常见应用(3)
一.统一全站字符编码 通过配置参数charset指明使用何种字符编码,以处理Html Form请求参数的中文问题 package me.gacl.web.filter; import java.io. ...
- Kotlin初探
前几天看到新闻,Google将Kotlin语言作为Android应用开发的一级语言, 与Java并驾齐驱, 这则消息在开发界一下就炸开了锅( 好像平息的很快...)! 连Google的亲儿子go语言也 ...
- LindAgile.Modules模块化的设计
在LindAgile中有一个比较主推的技术,就是模块化,一切组件都可以被抽象成一个小小的模块,而每个小模块的实现可能又有多种方式,如日志模块可以有LindLoger,Log4net等实现,而具体在程序 ...
- R语言 模糊c均值(FCM)算法程序(转)
FCM <- function(x, K, mybeta = 2, nstart = 1, iter_max = 100, eps = 1e-06) { ## FCM ## INPUTS ## ...
- javascript闭包小结
对比看了几本书关于闭包的介绍,感觉<JavaScript面向对象编程指南>一书中介绍的最为清楚,其他部分书籍很多只讲结果,不讲具体原因,不易理解.总结如下 1.闭包的定义 如果一个函数会在 ...
- 傻瓜式安装nginx以及负载均衡配置
概述 需求 做了一个对内的http api应用.由于只有一台服务器,考虑到升级问题(即升级时会造成几秒钟用户访问不了),决定搭一个nginx,公共端口:9999,部署2套应用,端口:9981,9982 ...