{"id":8558,"date":"2021-08-09T23:27:59","date_gmt":"2021-08-10T06:27:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8558"},"modified":"2021-08-09T23:43:41","modified_gmt":"2021-08-10T06:43:41","slug":"leetcode-1896-minimum-cost-to-change-the-final-value-of-expression","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/recursion\/leetcode-1896-minimum-cost-to-change-the-final-value-of-expression\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1896. Minimum Cost to Change the Final Value of Expression"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>valid<\/strong>&nbsp;boolean expression as a string&nbsp;<code>expression<\/code>&nbsp;consisting of the characters&nbsp;<code>'1'<\/code>,<code>'0'<\/code>,<code>'&amp;'<\/code>&nbsp;(bitwise&nbsp;<strong>AND<\/strong>&nbsp;operator),<code>'|'<\/code>&nbsp;(bitwise&nbsp;<strong>OR<\/strong>&nbsp;operator),<code>'('<\/code>, and&nbsp;<code>')'<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>\"()1|1\"<\/code>&nbsp;and&nbsp;<code>\"(1)&amp;()\"<\/code>&nbsp;are&nbsp;<strong>not valid<\/strong>&nbsp;while&nbsp;<code>\"1\"<\/code>,&nbsp;<code>\"(((1))|(0))\"<\/code>, and&nbsp;<code>\"1|(0&amp;(1))\"<\/code>&nbsp;are&nbsp;<strong>valid<\/strong>&nbsp;expressions.<\/li><\/ul>\n\n\n\n<p>Return<em>&nbsp;the&nbsp;<strong>minimum cost<\/strong>&nbsp;to change the final value of the expression<\/em>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>expression = \"1|1|(0&amp;0)&amp;1\"<\/code>, its&nbsp;<strong>value<\/strong>&nbsp;is&nbsp;<code>1|1|(0&amp;0)&amp;1 = 1|1|0&amp;1 = 1|0&amp;1 = 1&amp;1 = 1<\/code>. We want to apply operations so that the<strong>&nbsp;new<\/strong>&nbsp;expression evaluates to&nbsp;<code>0<\/code>.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>cost<\/strong>&nbsp;of changing the final value of an expression is the&nbsp;<strong>number of operations<\/strong>&nbsp;performed on the expression. The types of&nbsp;<strong>operations<\/strong>&nbsp;are described as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Turn a&nbsp;<code>'1'<\/code>&nbsp;into a&nbsp;<code>'0'<\/code>.<\/li><li>Turn a&nbsp;<code>'0'<\/code>&nbsp;into a&nbsp;<code>'1'<\/code>.<\/li><li>Turn a&nbsp;<code>'&amp;'<\/code>&nbsp;into a&nbsp;<code>'|'<\/code>.<\/li><li>Turn a&nbsp;<code>'|'<\/code>&nbsp;into a&nbsp;<code>'&amp;'<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;<code>'&amp;'<\/code>&nbsp;does&nbsp;<strong>not<\/strong>&nbsp;take precedence over&nbsp;<code>'|'<\/code>&nbsp;in the&nbsp;<strong>order of calculation<\/strong>. Evaluate parentheses&nbsp;<strong>first<\/strong>, then in&nbsp;<strong>left-to-right<\/strong>&nbsp;order.<\/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 = \"1&amp;(0|1)\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We can turn \"1&amp;(0<strong>|<\/strong>1)\" into \"1&amp;(0<strong>&amp;<\/strong>1)\" by changing the '|' to a '&amp;' using 1 operation.\nThe new expression evaluates to 0. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> expression = \"(0&amp;0)&amp;(0&amp;0&amp;0)\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> We can turn \"(0<strong>&amp;0<\/strong>)<strong><u>&amp;<\/u><\/strong>(0&amp;0&amp;0)\" into \"(0<strong>|1<\/strong>)<strong>|<\/strong>(0&amp;0&amp;0)\" using 3 operations.\nThe new expression evaluates to 1.\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 = \"(0|(1|0&amp;1))\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We can turn \"(0|(<strong>1<\/strong>|0&amp;1))\" into \"(0|(<strong>0<\/strong>|0&amp;1))\" using 1 operation.\nThe new expression evaluates to 0.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= expression.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>expression<\/code>&nbsp;only contains&nbsp;<code>'1'<\/code>,<code>'0'<\/code>,<code>'&amp;'<\/code>,<code>'|'<\/code>,<code>'('<\/code>, and&nbsp;<code>')'<\/code><\/li><li>All parentheses&nbsp;are properly matched.<\/li><li>There will be no empty parentheses (i.e:&nbsp;<code>\"()\"<\/code>&nbsp;is not a substring of&nbsp;<code>expression<\/code>).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP, Recursion \/ Simulation w\/ Stack<\/strong><\/h2>\n\n\n\n<p>For each expression, stores the min cost to change value to 0 and 1.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minOperationsToFlip(string expression) {\n    stack<array<int, 3>> s;\n    s.push({0, 0, 0});\n    for (char e : expression) {\n      if (e == '(')\n        s.push({0, 0, 0});\n      else if (e == '&' || e == '|')\n        s.top()[2] = e;\n      else {        \n        if (isdigit(e)) s.push({e != '0', e != '1', 0});\n        auto [r0, r1, _] = s.top(); s.pop();\n        auto [l0, l1, op] = s.top(); s.pop();\n        if (op == '&') {\n          s.push({min(l0, r0),\n                  min(l1 + r1, min(l1, r1) + 1),\n                  0});\n        } else if (op == '|') {\n          s.push({min(l0 + r0, min(l0, r0) + 1),\n                  min(l1, r1),\n                  0});\n        } else {\n          s.push({r0, r1, 0});\n        }\n      }\n    }\n    return max(s.top()[0], s.top()[1]);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;valid&nbsp;boolean expression as a string&nbsp;expression&nbsp;consisting of the characters&nbsp;&#8216;1&#8217;,&#8217;0&#8242;,&#8217;&amp;&#8217;&nbsp;(bitwise&nbsp;AND&nbsp;operator),&#8217;|&#8217;&nbsp;(bitwise&nbsp;OR&nbsp;operator),'(&#8216;, and&nbsp;&#8216;)&#8217;. For example,&nbsp;&#8220;()1|1&#8221;&nbsp;and&nbsp;&#8220;(1)&amp;()&#8221;&nbsp;are&nbsp;not valid&nbsp;while&nbsp;&#8220;1&#8221;,&nbsp;&#8220;(((1))|(0))&#8221;, and&nbsp;&#8220;1|(0&amp;(1))&#8221;&nbsp;are&nbsp;valid&nbsp;expressions. Return&nbsp;the&nbsp;minimum cost&nbsp;to change the final value of the expression.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[18,173,17],"class_list":["post-8558","post","type-post","status-publish","format-standard","hentry","category-recursion","tag-dp","tag-expression","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8558","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=8558"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8558\/revisions"}],"predecessor-version":[{"id":8561,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8558\/revisions\/8561"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}