add: 220227-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-02-27 15:59:15 +08:00
parent 77ebc0944e
commit c94fb64297
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 35 additions and 1 deletions

34
cpp/2202/220227-CN.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <vector>
#include <string>
#include <iostream>
/**
* 553. Optimal Division
* You are given an integer array nums. The adjacent integers in nums will perform the float division.
* For example, for nums = [2,3,4], we will evaluate the expression "2/3/4".
* However, you can add any number of parenthesis at any position to change the priority of operations. You want to add these parentheses such the value of the expression after the evaluation is maximum.
* Return the corresponding expression that has the maximum value in string format.
* Note: your expression should not contain redundant parenthesis.
*/
class Solution {
public:
static std::string optimalDivision(const std::vector<int>& nums) {
int n = nums.size();
if (n == 1)
return std::to_string(nums[0]);
else if (n == 2)
return std::to_string(nums[0]) + "/" + std::to_string(nums[1]);
std::string ret = std::to_string(nums[0]) + "/(";
for (int i = 1; i < n; ++i) {
ret += std::to_string(nums[i]) + "/";
}
ret.back() = ')';
return ret;
}
};
int main() {
std::cout << Solution::optimalDivision({1000, 100, 10, 2});
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220226.cpp)
ADD_EXECUTABLE(2202 220227-CN.cpp)