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. 第二章 基本图像处理(Image Processing)

    主要内容: 图像的表示----介绍图像是如何表示的,以及所有基本操作的作用对象 高斯滤波-----滤波操作的原理与应用 图像金字塔-----高斯和拉普拉斯 边缘检测-----Sobel算子和Lapla ...

  2. Java学习笔记——设计模式之三.装饰模式

    函数应该做一件事,做好这件事,只做这一件事. --Clean Code 装饰模式,上代码: 先定义零件类: package cn.no3.decorator.template; public abst ...

  3. libsvm参数选择

    以前接触过libsvm,现在算在实际的应用中学习 LIBSVM 使用的一般步骤是: 1)按照LIBSVM软件包所要求的格式准备数据集: 2)对数据进行简单的缩放操作: 3)首要考虑选用RBF 核函数: ...

  4. JS 中new一个对象发生了什么事

    今天看到一个360的前端面试题: function A(){}function B(a){  this.a=a;}function C(a){  if(a){    this.a=a;   }}A.p ...

  5. Java IO详解(六)------随机访问文件流

    File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...

  6. SparkMLlib分类算法之支持向量机

    SparkMLlib分类算法之支持向量机 (一),概念 支持向量机(support vector machine)是一种分类算法,通过寻求结构化风险最小来提高学习机泛化能力,实现经验风险和置信范围的最 ...

  7. Spring-AOP用法总结

    前言     Spring AOP的实现方法很多,在项目开发中具体采用什么方式,需要按实际情况来选择,每一种的用法,有其一定的实用价值,所以本文将各种使用方法进行了具体实现.主要包括Advice的be ...

  8. .Net程序员学用Oracle系列(10):系统函数(中)

    1.四大转换函数 1.1.TO_CHAR 1.2.TO_NUMBER 1.3.TO_DATE 1.4.CAST 2.两大近似值函数 2.1.ROUND 2.2.TRUNC 3.正则函数 3.1.正则函 ...

  9. 使用ThreadLocal实现的读写分离在迁移后的偶发错误

    最近莫名的会有错误日志,说有写操作因为走了读库而报了read only的异常,由于并没有造成应用使用的问题,开始我以为哪的配置错误就没当回事让程序员自己去查了,然而... 背景:之前的博客里提到过,读 ...

  10. 如何在不使用系统函数的情况下实现PHP中数组系统函数的功能

    PHP中为我们提供了各种各样的系统函数来实现我们需要的各种功能,那么,在不使用系统函数的情况下我们要怎样来实现这些功能呢?以下就是几种系统函数的实现方式. 首先,我们来定义一个数组: $arr= ar ...