[leetcode]553. Optimal Division
Analysis
啊啊啊啊啊啊—— [生死有命富贵在天]
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.
一开始想了很多,暴力啊,DP啊,最后发现只是个数学问题,这个博客解释的比较清楚:https://blog.csdn.net/hiroshiten/article/details/72758435
Implement
class Solution {
public:
string optimalDivision(vector<int>& nums) {
int len = nums.size();
string res = "";
for(int i=0; i<len; i++){
if(i > 0)
res += "/";
if(i == 1 && len>2)
res += "(";
res += to_string(nums[i]);
if(i == len-1 && len>2)
res += ")";
}
return res;
}
};