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]的更多相关文章

  1. 【LeetCode】553. Optimal Division 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 553. Optimal Division

    题目: Given a list of positive integers, the adjacent integers will perform the float division. For ex ...

  3. LC 553. Optimal Division

    Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...

  4. 【leetcode】553. Optimal Division

    题目如下: 解题思路:这是数学上的一个定理.对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN). 代码如下: class Solution(obj ...

  5. [LeetCode] Optimal Division 最优分隔

    Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...

  6. LeetCode Optimal Division

    原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...

  7. [Swift]LeetCode553. 最优除法 | Optimal Division

    Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...

  8. [Leetcode Week3]Evaluate Division

    Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...

  9. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  10. [LeetCode] 465. Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

随机推荐

  1. 受够了if (ModelState.IsValid)?ActionFitlter也是一路的坑啊!

    这篇博客真是干货,干得估计还有点“磕牙”,所以还提供视频和代码.但基础稍弱的同学,怕还是得自行补充一些基础知识——就一篇文章,确实没办法面面俱到. 视频和代码下载:Demo - 百度云盘 · 一起帮 ...

  2. Windows下安装Nodejs步骤

      最近打算把我们的微信端用Vue.js重构,为什么选择Vue.js,一是之前使用的是传统的asp.net mvc,多页面应用用户体验比单页面要差.二是使用过Angular.js,感觉对开发人员要求较 ...

  3. OAuth及第三方登录

    现在的生活中运用互联网的有好多地方,我们既要申请微博,申请博客,申请邮箱等等:哪怕登录一个小网址看点东西都要注册登录,不过现在好多了:有了第三方登录,再也不用担心这不够用的脑子整天记忆账号和密码了,只 ...

  4. JDFS:一款分布式文件管理实用程序第一篇(线程池、epoll、上传、下载)

    一 前言 截止目前,笔者在博客园上面已经发表了3篇关于网络下载的文章,这三篇博客实现了基于socket的http多线程远程断点下载实用程序.笔者打算在此基础上开发出一款分布式文件管理实用程序,截止目前 ...

  5. 14、Iterator跟ListIterator的区别

    14.Iterator与ListIterator的区别 在使用List,Set的时候,为了实现对其数据的遍历,会经常使用到Iterator(跌代器).使用跌代器,不需要干涉其遍历的过程,只需要每次取出 ...

  6. swift学习 - 单例实现(singleton)

    swift中实现单例的方式 class LGConfig: NSObject { static let instance = LGConfig() private override init() { ...

  7. struts2.1.6教程八、验证机制

    注意:要想实现校验,action必须继承自ActionSupport类. 1.基于手工编码的校验 我们建立struts2validate项目 ,其中reg.jsp页面主要代码如下: <body& ...

  8. 入坑以来最清晰的this指南[老哥们来交流指正]

    直接放有道云笔记的链接,博客园的markdown总是用不好. 1.这一篇是this的绑定(call,apply,bind) http://note.youdao.com/noteshare?id=c3 ...

  9. 【.net 深呼吸】通过标准输入/输出流来完成进程间通信

    实现进程之间煲电话粥的方式,有好几种,比如,你可以用这些方案: 1.使用socket来传递.这个好像很无聊,本地进程之间也用socket?不过,通过本机回环网络确实可以进程之间通信. 2.WCF,与上 ...

  10. iOS安全攻防之阻止GDB依附

    GDB是大多数hackers的首选,阻止GDB依附到应用的常规办法是: #import <dlfcn.h> #import <sys/types.h> typedef int ...