LC 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.
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的更多相关文章
- 553. Optimal Division
题目: Given a list of positive integers, the adjacent integers will perform the float division. For ex ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- C++ void*解惑
最近遇到void *的问题无法解决,发现再也无法逃避了(以前都是采取悄悄绕过原则),于是我决定直面它. 在哪遇到了? 线程创建函数pthread_create()的最后一个参数void *arg,嗯? ...
- (十三)Linux sysfs device_attribute
/*************************************************************************** * Linux sysfs device_at ...
- TCP的ACK原理和延迟确认机制
某天晚上睡觉前突然想到 tcp的ACK确认是单独发的还是和报文一起发的,下面看一下别人的解答 一.ACK定义TCP协议中,接收方成功接收到数据后,会回复一个ACK数据包,表示已经确认接收到ACK确认号 ...
- CSS基础学习-6.CSS属性_列表、表格
- Java 实现的 简单WordCount功能
githup 链接:https://gitee.com/iy2524/WordCount.git PSP表格 psp2.1 psp阶段 估计耗时(分钟) 实际耗时(分钟) Planning 计 ...
- Liquibase使用(转)
文章目录 介绍快速使用Springboot中引入依赖配置日志文件ChangeLog编写变更记录ChangeSetMaven中引入依赖配置liquibase.properties编写变更记录Change ...
- Mybatis问题-Type interface com.zzu.ssm.dao.UserMapper is not known to the MapperRegistry
1. mapper.xml中namespace名称是否与dao接口包名一致 2. 在mybatis配置文件中注册mapper
- js比较两个时间的大小
function checkdate(s,e){ //得到日期值并转化成日期格式,replace(/-/g, "//")是根据验证表达式把日期转化成长日期格式,这样再进行判断就好判 ...
- LibreOffice/Calc:带条件判断的求和
本文适用于LibreOffice Calc 5.1.6.2 + Ubuntu 16.04,熊猫帮帮主@cnblogs 2018/3/7 以下图为例,假设要根据C列对D列中被选中单元进行求和,即对D列中 ...
- codevs3269 混合背包 x
3269 混合背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 背包体积为V ,给出N个物品,每个物品占用体积为 ...