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 ...
随机推荐
- php--常见算法1
<?php //递归输出123321 function digui($num){ echo $num; if($num<3){ digui($num+1); } echo $num; } ...
- MySQL数据库笔记三:数据查询语言(DQL)与事务控制语言(TCL)
五.数据查询语言(DQL) (重中之重) 完整语法格式: select 表达式1|字段,.... [from 表名 where 条件] [group by 列名] [having 条件] [order ...
- odoo 关系字段(关联关系)
Many-to-one关联 publisher_id = fields.Many2one(comodel_name= 'res.partner', domain='',context={},ondel ...
- Django简介及安装
Django简介及安装 我们都知道,Django是一种基于Python的Web开发框架. 那么,什么是Web开发?Web开发指的是开发基于B/S架构,通过前后端的配合,将后台服务器的数据在浏览器上展现 ...
- Linux 之Ubuntu在VM中安装(桌面版)
1.安装系统 https://jingyan.baidu.com/article/14bd256e0ca52ebb6d26129c.html 2.安装VM Tools https://jingyan. ...
- POJ1722 算法竞赛进阶指南 SUBSTRACT减操作
原题连接 题目描述 给定一个整数数组\(a_1,a_2,-,a_n\). 定义数组第 i 位上的减操作:把\(a_i\)和\(a_{i+1}\)换成\(a_i - a_{i+1}\). 用con(a, ...
- D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...
- Easy Populate批量管理下载产品数据为空的解决办法
把原来的先删除:http://aaaaacom/admin/easypopulate.php?langer=remove
- python+Appium自动化:记录遇到的坑
1.打开 uiautomatorviewer同步的的时候突然报错 Error while obtaining UI hierarchy XML file: com.android.ddmlib.Syn ...
- Java-JDBCUtil工具类
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...