{"id":9636,"date":"2022-04-09T23:20:27","date_gmt":"2022-04-10T06:20:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9636"},"modified":"2022-04-09T23:22:26","modified_gmt":"2022-04-10T06:22:26","slug":"leetcode-2232-minimize-result-by-adding-parentheses-to-expression","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2232-minimize-result-by-adding-parentheses-to-expression\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2232. Minimize Result by Adding Parentheses to Expression"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>expression<\/code>&nbsp;of the form&nbsp;<code>\"&lt;num1&gt;+&lt;num2&gt;\"<\/code>&nbsp;where&nbsp;<code>&lt;num1&gt;<\/code>&nbsp;and&nbsp;<code>&lt;num2&gt;<\/code>&nbsp;represent positive integers.<\/p>\n\n\n\n<p>Add a pair of parentheses to&nbsp;<code>expression<\/code>&nbsp;such that after the addition of parentheses,&nbsp;<code>expression<\/code>&nbsp;is a&nbsp;<strong>valid<\/strong>&nbsp;mathematical expression and evaluates to the&nbsp;<strong>smallest<\/strong>&nbsp;possible value. The left parenthesis&nbsp;<strong>must<\/strong>&nbsp;be added to the left of&nbsp;<code>'+'<\/code>&nbsp;and the right parenthesis&nbsp;<strong>must<\/strong>&nbsp;be added to the right of&nbsp;<code>'+'<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>expression<\/code><em>&nbsp;after adding a pair of parentheses such that&nbsp;<\/em><code>expression<\/code><em>&nbsp;evaluates to the&nbsp;<strong>smallest<\/strong>&nbsp;possible value.<\/em>&nbsp;If there are multiple answers that yield the same result, return any of them.<\/p>\n\n\n\n<p>The input has been generated such that the original value of&nbsp;<code>expression<\/code>, and the value of&nbsp;<code>expression<\/code>&nbsp;after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> expression = \"247+38\"\n<strong>Output:<\/strong> \"2(47+38)\"\n<strong>Explanation:<\/strong> The <code>expression<\/code> evaluates to 2 * (47 + 38) = 2 * 85 = 170.\nNote that \"2(4)7+38\" is invalid because the right parenthesis must be to the right of the <code>'+'<\/code>.\nIt can be shown that 170 is the smallest possible value.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> expression = \"12+34\"\n<strong>Output:<\/strong> \"1(2+3)4\"\n<strong>Explanation:<\/strong> The expression evaluates to 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> expression = \"999+999\"\n<strong>Output:<\/strong> \"(999+999)\"\n<strong>Explanation:<\/strong> The <code>expression<\/code> evaluates to 999 + 999 = 1998.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= expression.length &lt;= 10<\/code><\/li><li><code>expression<\/code>&nbsp;consists of digits from&nbsp;<code>'1'<\/code>&nbsp;to&nbsp;<code>'9'<\/code>&nbsp;and&nbsp;<code>'+'<\/code>.<\/li><li><code>expression<\/code>&nbsp;starts and ends with digits.<\/li><li><code>expression<\/code>&nbsp;contains exactly one&nbsp;<code>'+'<\/code>.<\/li><li>The original value of&nbsp;<code>expression<\/code>, and the value of&nbsp;<code>expression<\/code>&nbsp;after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Try all possible positions to add parentheses and evaluate the new expression.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string minimizeResult(string expression) {\n    const int n = expression.size();\n    auto p = expression.find('+');\n    int best = INT_MAX;\n    string ans;\n    for (int l = 0; l < p; ++l)\n      for (int r = p + 2; r < n + 1; ++r) {\n        const int m1 = l ? stoi(expression.substr(0, l)) : 1;\n        const int m2 = (r < n) ? stoi(expression.substr(r)) : 1;\n        const int n1 = stoi(expression.substr(l, p));\n        const int n2 = stoi(expression.substr(p + 1, r - p - 1));\n        const int cur = m1 * (n1 + n2) * m2;\n        if (cur < best) {\n          best = cur;\n          ans = expression;\n          ans.insert(l, \"(\");\n          ans.insert(r + 1, \")\");\n        }\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;string&nbsp;expression&nbsp;of the form&nbsp;&#8220;&lt;num1&gt;+&lt;num2&gt;&#8221;&nbsp;where&nbsp;&lt;num1&gt;&nbsp;and&nbsp;&lt;num2&gt;&nbsp;represent positive integers. Add a pair of parentheses to&nbsp;expression&nbsp;such that after the addition of parentheses,&nbsp;expression&nbsp;is a&nbsp;valid&nbsp;mathematical expression and evaluates to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[173,177,421,4],"class_list":["post-9636","post","type-post","status-publish","format-standard","hentry","category-string","tag-expression","tag-medium","tag-parentheses","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9636"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9636\/revisions"}],"predecessor-version":[{"id":9638,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9636\/revisions\/9638"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}