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. makefile文件模板介绍

    1    src : = $(shell  ls  *.c)2    objs : = $(patsubst  %.c, %.o, $(src))3    test : $(objs)4       ...

  2. 编程语言与C语言的简介

    1.什么是程序 指挥计算机执行我们想要它做的动作,而依照顺序执行的一组指令 2.程序的作用是什么 指挥计算机工作 3.程序的特征 1.程序是一行一行的执行 2.是一种与计算机沟通的语言 3.程序是由特 ...

  3. spring-定时器(2)

    Spring提供的三种定时任务机制及其比较 定时任务的需求在众多应用系统中广泛存在,在Spring中,我们可以使用三种不同的定时机制,下面一一描述并加以比较 1. 基于Quartz的定时机制 下面详细 ...

  4. 侯捷STL学习(一)

    开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...

  5. MyBatis-3.2.2

    note SqlSessionFactory 它是一个线程安全的 SqlSession 线程非安全,不能做类的公用变量 当数据库字段和实体对象名称不一至时,通过sql的字段命名别名,别名跟实体对象属性 ...

  6. python 多线程,进程的理解

    python的threading.Thread类有一个run方法,用于定义线程的功能函数,可以在自己的线程类中覆盖该方法.而创建自己的线程实例后,通过Thread类的start方法,可以启动该线程,交 ...

  7. C语言之运算符和条件结构

    表达式:是有操作数和运算符组成的. 操作数:常量.变量.子表达式 X=(x+2)*(y-2); 运算符: 赋值运算符:= .其作用是做赋值运算,将等号后边的值赋值给等号前边的. 复合赋值运算符: += ...

  8. 为什么很多人使用#define而不是const定义常量

    众所周知,C语言一开始只有#define,C程序员用#define定义符号常量.但后来ANSI C加入了const限定符,而const应该比#define更好,为什么现在的C程序员还在大量使用#def ...

  9. 腾讯AlloyTeam正式发布omi-cli脚手架 v1.0 - 创建网站无需任何配置

    omi-cli omi-cli omi-cli命令 omi框架 用户指南 文件目录 npm 脚本 npm start npm run dist 代码分割 兼容 IE8 插入 CSS 插入组件局部 CS ...

  10. 一天搞定CSS(扩展):CSS Hack

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...