Givena list of positiveintegers, the adjacent integers will performthe 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 ofoperations. 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:
1. The length of the input array is [1,10].
2. Elements in the given array will bein range [2, 1000].
3. There is only one optimal divisionfor each test case.
class Solution {
public:
void theGet(map<int, pair<int, int>> &theMark, vector < vector<pair<float, int>>> & dp, vector < vector<pair<float, int>>> & dpMin, bool useWhat,int start, int end)
{
if (start - end + 1 == 1 || start - end + 1 == 2) return;
int mid;
if(useWhat)
mid = dp[start][end].second;
else
{
mid = dpMin[start][end].second;
}
if (mid != end - 1)
{
theMark[mid].second++;
theMark[end].first++;
theGet(theMark, dp,dpMin,useWhat, start, mid);
theGet(theMark, dp,dpMin,!useWhat, mid+1, end);
}
else
{
theGet(theMark, dp,dpMin, useWhat,start,mid);
}
}
string optimalDivision(vector<int>& nums) {
int n = nums.size();
vector < vector<pair<float, int>>> dp(n, vector<pair<float, int>>(n));
vector < vector<pair<float, int>>> dpMin(n, vector<pair<float, int>>(n));
float maxTmp; int maxIndex; float minTmp; int minIndex;
for (int i = 0; i < n; i++)
{
dp[i][i].first = nums[i];
dp[i][i].second = -1;
dpMin[i][i].first= nums[i];;
dpMin[i][i].second=-1;
}
for(int l=2;l<=n;l++)
for (int i = 0; i <= n - l; i++)
{
maxTmp = -1; minTmp = INT_MAX;
int j;
for (j = 1; j < l; j++)
{
int mid, right, left;
mid = i + j - 1;
right = i + l - 1;
float l = dp[i][mid].first / dpMin[mid + 1][right].first;
float l2= dpMin[i][mid].first / dp[mid + 1][right].first;
if (l > maxTmp)
{
maxTmp = l;
maxIndex = j;
}
if (l2 < minTmp)
{
minTmp = l2;
minIndex = j;
}
}
dp[i][i + l - 1].first = maxTmp;
dp[i][i + l - 1].second = i+maxIndex-1;
dpMin[i][i + l - 1].first = minTmp;
dpMin[i][i + l - 1].second = i + minIndex - 1;
}
map<int, pair<int, int>> theMark;
theMark[n - 1] = pair<int, int>(0, 0);
theGet(theMark, dp,dpMin,true,0, n - 1);
int index = 0;
map<int, pair<int, int>> ::iterator it = theMark.begin();
string result="";
while (it != theMark.end())
{
int l = (*it).first; int t;
for (; index <= l; index++)
{
result.append(to_string(nums[index]));
if(index!=l)result.push_back('/');
}
l = 0; while (l < (*it).second.first) { result.push_back(')'); l++; }
if ((*it).first != n-1) result.push_back('/');
l = 0; while (l < (*it).second.second) { result.push_back('('); l++; }
it++;
}
return result;
}
};