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:

  1. The length of the input array is [1, 10].
  2. Elements in the given array will be in range [2, 1000].
  3. There is only one optimal division for each test case.

这道题给了我们一个数组,让我们确定除法的顺序,从而得到值最大的运算顺序,并且不能加多余的括号。刚开始博主没看清题,以为是要返回最大的值,就直接写了个递归的暴力搜索的方法,结果发现是要返回带括号的字符串,尝试的修改了一下,觉得挺麻烦。于是直接放弃抵抗,上网参考大神们的解法,结果大吃一惊,这题原来还可以这么解,完全是数学上的知识啊,太tricky了。数组中n个数字,如果不加括号就是:

x1 / x2 / x3 / ... / xn

那么我们如何加括号使得其值最大呢,那么就是将x2后面的除数都变成乘数,比如只有三个数字的情况 a / b / c,如果我们在后两个数上加上括号 a / (b / c),实际上就是a / b * c。而且b永远只能当除数,a也永远只能当被除数。同理,x1只能当被除数,x2只能当除数,但是x3之后的数,只要我们都将其变为乘数,那么得到的值肯定是最大的,所以就只有一种加括号的方式,即:

x1 / (x2 / x3 / ... / xn)

这样的话就完全不用递归了,这道题就变成了一个道简单的字符串操作的题目了,这思路,博主服了,参见代码如下:

解法一:

class Solution {
public:
string optimalDivision(vector<int>& nums) {
if (nums.empty()) return "";
string res = to_string(nums[]);
if (nums.size() == ) return res;
if (nums.size() == ) return res + "/" + to_string(nums[]);
res += "/(" + to_string(nums[]);
for (int i = ; i < nums.size(); ++i) {
res += "/" + to_string(nums[i]);
}
return res + ")";
}
};

下面这种解法的思路和上面基本相同,就是写法上略有不同,直接看代码吧:

解法二:

class Solution {
public:
string optimalDivision(vector<int>& nums) {
string res = "";
int n = nums.size();
for (int i = ; i < n; ++i) {
if (i > ) res += "/";
if (i == && n > ) res += "(";
res += to_string(nums[i]);
if (i == n - && n > ) res += ")";
}
return res;
}
};

参考资料:

https://discuss.leetcode.com/topic/86487/c-java-clean-code

https://discuss.leetcode.com/topic/86483/easy-to-understand-simple-o-n-solution-with-explanation/2

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Optimal Division 最优分隔的更多相关文章

  1. LeetCode Optimal Division

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

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

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

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

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

  4. 553. Optimal Division

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

  5. LC 553. Optimal Division

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

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

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

  7. 【leetcode】553. Optimal Division

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

  8. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  9. Leetcode: Optimal Account Balancing

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

随机推荐

  1. Android学习链接大放送

    虽然贴链接这种事情..真是一种很偷懒的做法... 但是我一个小菜鸟,果断还是要以多向别人学习为主... 好资源要和大家分享对不对! 况且..放博客里..比收藏夹的利用几率要大一点! 原作者应该也很喜欢 ...

  2. android 滑动分页

    import android.app.ListActivity;import android.os.Bundle;import android.os.Handler;import android.vi ...

  3. 深入理解Java对象的创建过程:类的初始化与实例化

    摘要: 在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一点是Java规范规定的.在实例化一个对象时,JVM首先会检查相关类型是否已经加载并初始化,如果没有,则JVM立即进行加载并调用类 ...

  4. python中Properties的一些小用法

    property最大的用处就是可以为一个属性制定getter,setter,delete和doc,他的函数原型为: def __init__(self, fget=None, fset=None, f ...

  5. Ubuntu16.04安装postgresql9.4及pgadmin3图形管理界面

    参考原文链接:http://www.cnblogs.com/sparkdev/p/5678874.html 安装前的检查 首先查看是否已经安装了旧版本: dpkg -l |grep postgresq ...

  6. 第一次作业:来自一个奋斗的IT学子

    第一部分 结缘计算机 1.1你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?(必答) 说起为何结缘了计算机,就得谈谈专业报考了,我觉得我的报考真是一个反面教科书了.由于高中以前每天只要想着 ...

  7. 105&250-高级软件工程2017第3次作业

    小组成员 2017282110250 王婷婷 2017202110105 张芷祎 github地址 https://github.com/setezzy/Calculator_GUI PSP PSP2 ...

  8. java第5章学习总结

    学号20145336 <Java程序设计>第5周学习总结 教材学习内容总结 try catch JVM会先尝试执行try区块中的内容,若发生错误且与catch后面的类型相符,则执行catc ...

  9. Twisted 延迟调用

    延迟(defer)是twisted框架中实现异步的编程体系,使程序设计可以采用事件驱动的机制 1.基本使用 defer可以看作一个管理回调函数的对象,可以向该对象添加需要的回调函数同时也可以指定该组函 ...

  10. 《高级软件测试》11.14.安装和运行Jira

    今日任务完成情况如下: 小段:研究Jira在Linux的安装教程 小费:尝试在Ubuntu下安装Jira 小高:查阅了关于Jira软件的介绍和安装教程,下载准备明天安装,并学习使用 小王:注册Jira ...