{"id":7474,"date":"2020-10-10T21:56:20","date_gmt":"2020-10-11T04:56:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7474"},"modified":"2020-10-10T21:59:49","modified_gmt":"2020-10-11T04:59:49","slug":"leetcode-1614-maximum-nesting-depth-of-the-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-1614-maximum-nesting-depth-of-the-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1614. Maximum Nesting Depth of the Parentheses"},"content":{"rendered":"\n<p>A string is a&nbsp;<strong>valid parentheses string<\/strong>&nbsp;(denoted&nbsp;<strong>VPS<\/strong>) if it meets one of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It is an empty string&nbsp;<code>\"\"<\/code>, or a single character not equal to&nbsp;<code>\"(\"<\/code>&nbsp;or&nbsp;<code>\")\"<\/code>,<\/li><li>It can be written as&nbsp;<code>AB<\/code>&nbsp;(<code>A<\/code>&nbsp;concatenated with&nbsp;<code>B<\/code>), where&nbsp;<code>A<\/code>&nbsp;and&nbsp;<code>B<\/code>&nbsp;are&nbsp;<strong>VPS<\/strong>&#8216;s, or<\/li><li>It can be written as&nbsp;<code>(A)<\/code>, where&nbsp;<code>A<\/code>&nbsp;is a&nbsp;<strong>VPS<\/strong>.<\/li><\/ul>\n\n\n\n<p>We can similarly define the&nbsp;<strong>nesting depth<\/strong>&nbsp;<code>depth(S)<\/code>&nbsp;of any VPS&nbsp;<code>S<\/code>&nbsp;as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>depth(\"\") = 0<\/code><\/li><li><code>depth(A + B) = max(depth(A), depth(B))<\/code>, where&nbsp;<code>A<\/code>&nbsp;and&nbsp;<code>B<\/code>&nbsp;are&nbsp;<strong>VPS<\/strong>&#8216;s<\/li><li><code>depth(\"(\" + A + \")\") = 1 + depth(A)<\/code>, where&nbsp;<code>A<\/code>&nbsp;is a&nbsp;<strong>VPS<\/strong>.<\/li><\/ul>\n\n\n\n<p>For example,&nbsp;<code>\"\"<\/code>,&nbsp;<code>\"()()\"<\/code>, and&nbsp;<code>\"()(()())\"<\/code>&nbsp;are&nbsp;<strong>VPS<\/strong>&#8216;s (with nesting depths 0, 1, and 2), and&nbsp;<code>\")(\"<\/code>&nbsp;and&nbsp;<code>\"(()\"<\/code>&nbsp;are not&nbsp;<strong>VPS<\/strong>&#8216;s.<\/p>\n\n\n\n<p>Given a&nbsp;<strong>VPS<\/strong>&nbsp;represented as string&nbsp;<code>s<\/code>, return&nbsp;<em>the&nbsp;<strong>nesting depth<\/strong>&nbsp;of&nbsp;<\/em><code>s<\/code>.<\/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> s = \"(1+(2*3)+((8)\/4))+1\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Digit 8 is inside of 3 nested parentheses in the string.\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> s = \"(1)+((2))+(((3)))\"\n<strong>Output:<\/strong> 3\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> s = \"1+(2*3)\/(2-1)\"\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1\"\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;consists of digits&nbsp;<code>0-9<\/code>&nbsp;and characters&nbsp;<code>'+'<\/code>,&nbsp;<code>'-'<\/code>,&nbsp;<code>'*'<\/code>,&nbsp;<code>'\/'<\/code>,&nbsp;<code>'('<\/code>, and&nbsp;<code>')'<\/code>.<\/li><li>It is guaranteed that parentheses expression&nbsp;<code>s<\/code>&nbsp;is a&nbsp;<strong>VPS<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>We only need to deal with &#8216;(&#8216; and &#8216;)&#8217;<\/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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxDepth(string s) {\n    int ans = 0;\n    int d = 0;\n    for (char c : s) {\n      if (c == '(') ans = max(ans, ++d);\n      else if (c == ')') --d;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A string is a&nbsp;valid parentheses string&nbsp;(denoted&nbsp;VPS) if it meets one of the following: It is an empty string&nbsp;&#8220;&#8221;, or a single character not equal to&nbsp;&#8220;(&#8221;&nbsp;or&nbsp;&#8220;)&#8221;,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[222,421,180,4],"class_list":["post-7474","post","type-post","status-publish","format-standard","hentry","category-stack","tag-easy","tag-parentheses","tag-stack","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7474","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=7474"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7474\/revisions"}],"predecessor-version":[{"id":7476,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7474\/revisions\/7476"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}