【LeetCode】553. Optimal Division 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/optimal-division/description/
题目描述
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.
题目大意
这个题的意思是在合适的位置添加上括号,使得最后得到的除法表达式值最大。
解题方法
这道题非常tricky,我们注意到除了第一个除数之外,后面的数都可以转变为乘积!!!
拿样例来说:
1000/(100/10/2) == (1000102)/(100)
所以,我们只需要考虑三种情况:
1.只有一个数,直接返回;
2.有两个数,第一个除以第二个返回;
3.有三个及以上的数,把第二个数后面的和第一个数全部乘起来,最后除以第二个数。(因为note当中说明了,给的数字都是[2,1000]的,所以第二个数后面的所有数乘起来都只会让结果变大)。
所以只要有超过两个的数值,那么最大的结果就是后面的括起来:
"1000/(100/10/2)"
"1000/(100/10/200)"
"1000/(100/1000/2)"
"1000/(100/10/20000)"
Python解法:
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
nums = map(str, nums)
if len(nums) <= 2:
return '/'.join(nums)
return '{}/({})'.format(nums[0], '/'.join(nums[1:]))
C++版本的代码如下:
class Solution {
public:
string optimalDivision(vector<int>& nums) {
string res = "";
const int N = nums.size();
if (N == 1) return to_string(nums[0]);
if (N == 2) return to_string(nums[0]) + '/' + to_string(nums[1]);
res += to_string(nums[0]) + "/(";
for (int i = 1; i < N - 1; i++) {
res += to_string(nums[i]) + "/";
}
res += to_string(nums[N - 1]) + ")";
return res;
}
};
日期
2018 年 2 月 28 日
2018 年 12 月 10 日 —— 又是周一!
【LeetCode】553. Optimal Division 解题报告(Python & C++)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- 42-Remove Nth Node From End of List
Remove Nth Node From End of List My Submissions QuestionEditorial Solution Total Accepted: 106592 To ...
- 2.MaxSubArray-Leetcode
题目:最大连续子序列和 思路:动态规划 状态转移方程 f[j]=max{f[j-1]+s[j],s[j]}, 其中1<=j<=n target = max{f[j]}, 其中1<=j ...
- GeckoDriver的安装和使用
GeckoDriver用于驱动Firefox,在这之前请确保已经正确安装好了Firefox浏览器并可以正常运行. 一.GeckoDriver的安装 GitHub:https://github.com/ ...
- 一文搞懂指标采集利器 Telegraf
作者| 姜闻名 来源|尔达 Erda 公众号 导读:为了让大家更好的了解 MSP 中 APM 系统的设计实现,我们决定编写一个<详聊微服务观测>系列文章,深入 APM 系统的产品.架构 ...
- 什么是 IP 地址 – 定义和解释
IP 地址定义 IP 地址是一个唯一地址,用于标识互联网或本地网络上的设备.IP 代表"互联网协议",它是控制通过互联网或本地网络发送的数据格式的一组规则. 本质上,IP 地址是允 ...
- Git配置文件与git config命令
在Git配置文件中配置变量,可以控制Git的外观和操作的各个方面.通过git config命令可以获得和设置配置变量. 一.Git配置文件的位置 这些变量可以被存储在三个不同的位置: 1./etc/g ...
- 3.0 rust 项目路径
$ rustc --versionrustc 1.44.0 (49cae5576 2020-06-01) 将代码存在到不同的文件 main.rs mod aa; fn main() { println ...
- Dubbo服务限流
为了防止某个消费者的QPS或是所有消费者的QPS总和突然飙升而导致的重要服务的失效,系统可以对访问流量进行控制,这种对集群的保护措施称为服务限流. Dubbo中能够实现服务限流的方式较多,可以划分为两 ...
- 基于jar的Spring Boot工程
一.Spring Boot简介 Spring Boot是由Pivotal[ˈpɪvətl]团队(一家做大数据的公司)提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架 ...
- js实现点击不同按钮切换内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...