From c94fb642972acfe1c38a94fc4f85644b8cc47159 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 27 Feb 2022 15:59:15 +0800 Subject: [PATCH] add: 220227-CN [cpp] --- cpp/2202/220227-CN.cpp | 34 ++++++++++++++++++++++++++++++++++ cpp/2202/CMakeLists.txt | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 cpp/2202/220227-CN.cpp diff --git a/cpp/2202/220227-CN.cpp b/cpp/2202/220227-CN.cpp new file mode 100644 index 0000000..2bb54ae --- /dev/null +++ b/cpp/2202/220227-CN.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +/** + * 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& 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; +} diff --git a/cpp/2202/CMakeLists.txt b/cpp/2202/CMakeLists.txt index f046b97..a1bf028 100644 --- a/cpp/2202/CMakeLists.txt +++ b/cpp/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220226.cpp) +ADD_EXECUTABLE(2202 220227-CN.cpp)