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.

Runtime: 4 ms, faster than 34.01% of C++ online submissions for Optimal Division.

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

LC 553. Optimal Division的更多相关文章

  1. 553. Optimal Division

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

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

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

  3. 【leetcode】553. Optimal Division

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

  4. [LeetCode] Optimal Division 最优分隔

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

  5. [Swift]LeetCode553. 最优除法 | 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. LC 465. Optimal Account Balancing 【lock,hard】

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

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

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. LeetCode Weekly Contest 28

    1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocode ...

随机推荐

  1. UNetbootin安装linux

    用u盘安装linux系统,最好的方法莫过于用UNetbootin,网址:http://unetbootin.github.io/ UNetbootin allows you to create boo ...

  2. WiFi 6是否适合个人用户

    5G已经被热炒得家喻户晓,但与其对标的WiFi新技术标准——Wi-Fi 6却可能有着更好的经济和技术前景,WiFi 6也称为802.11ax,比过去的Wi-Fi技术更好,速度更快能跟5G对标,连接性更 ...

  3. Python lambda 知识点

    作者说学会了lambda后,你会用上瘾的,因为让代码复用和简洁. 初识lamdba不太好理解,尤其是它能当着一个变量传递给函数,不过多学着写几个例子就好了,下面是我的学习笔记. lambda 操作符( ...

  4. PAT Basic 1056 组合数的和 (15 分)

    给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85 ...

  5. Type反射遍历类的属性

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup& ...

  6. linux抓取usb包设置usbmon

  7. Python的在线编辑环境

    另外,再提供几个Python的在线编辑环境,可以直接写代码并且运行的环境. 在线Python实验室:http://www.pythoner.cn/labs/ 在线Python编辑器:http://ww ...

  8. CSS基础学习-4.CSS属性_背景、颜色、边框

  9. kudu安装

    安装前提和准备: 硬件: 一台或者多台机器跑kudu-master.建议跑一个master(无容错机制).三个master(允许一个节点运行出错)或者五个master(允许两个节点出错). 一台或者多 ...

  10. freemarker 生成word

    一.生成模板,动态获取的部分用${变量名},然后将word另存为xml文件,再将后缀名改成ftl格式.然后将模板放在对应的目录下. 二.引入freemarker包,mawen引用 <depend ...