{"id":9609,"date":"2022-04-02T15:22:12","date_gmt":"2022-04-02T22:22:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9609"},"modified":"2022-04-02T15:22:58","modified_gmt":"2022-04-02T22:22:58","slug":"leetcode-2222-number-of-ways-to-select-buildings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2222-number-of-ways-to-select-buildings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2222. Number of Ways to Select Buildings"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;binary string&nbsp;<code>s<\/code>&nbsp;which represents the types of buildings along a street where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s[i] = '0'<\/code>&nbsp;denotes that the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;building is an office and<\/li><li><code>s[i] = '1'<\/code>&nbsp;denotes that the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;building is a restaurant.<\/li><\/ul>\n\n\n\n<p>As a city official, you would like to&nbsp;<strong>select<\/strong>&nbsp;3 buildings for random inspection. However, to ensure variety,&nbsp;<strong>no two consecutive<\/strong>&nbsp;buildings out of the&nbsp;<strong>selected<\/strong>&nbsp;buildings can be of the same type.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, given&nbsp;<code>s = \"0<u><strong>0<\/strong><\/u>1<u><strong>1<\/strong><\/u>0<u><strong>1<\/strong><\/u>\"<\/code>, we cannot select the&nbsp;<code>1<sup>st<\/sup><\/code>,&nbsp;<code>3<sup>rd<\/sup><\/code>, and&nbsp;<code>5<sup>th<\/sup><\/code>&nbsp;buildings as that would form&nbsp;<code>\"0<strong><u>11<\/u><\/strong>\"<\/code>&nbsp;which is&nbsp;<strong>not<\/strong>&nbsp;allowed due to having two consecutive buildings of the same type.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of valid ways<\/strong>&nbsp;to select 3 buildings.<\/em><\/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 = \"001101\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> \nThe following sets of indices selected are valid:\n- [0,2,4] from \"<strong>0<\/strong>0<strong><u>1<\/u><\/strong>1<strong><u>0<\/u><\/strong>1\" forms \"010\"\n- [0,3,4] from \"<strong>0<\/strong>01<strong>10<\/strong>1\" forms \"010\"\n- [1,2,4] from \"0<strong>01<\/strong>1<strong>0<\/strong>1\" forms \"010\"\n- [1,3,4] from \"0<strong>0<\/strong>1<strong>10<\/strong>1\" forms \"010\"\n- [2,4,5] from \"00<strong>1<\/strong>1<strong>01<\/strong>\" forms \"101\"\n- [3,4,5] from \"001<strong>101<\/strong>\" forms \"101\"\nNo other selection is valid. Thus, there are 6 total ways.\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 = \"11100\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> It can be shown that there are no valid selections.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s[i]<\/code>&nbsp;is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>The brute force solution will take O(n<sup>3<\/sup>) which will lead to TLE.<\/p>\n\n\n\n<p>Since the only two valid cases are &#8220;010&#8221; and &#8220;101&#8221;.<\/p>\n\n\n\n<p>We just need to count how many 0s and 1s, thus we can count 01s and 10s and finally 010s and 101s.<\/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  long long numberOfWays(string s) {    \n    long long c0 = 0, c1 = 0, c01 = 0, c10 = 0, c101 = 0, c010 = 0;\n    for (char c : s) {\n      if (c == '0') {\n        ++c0;\n        c10 += c1;\n        c010 += c01;\n      } else {\n        ++c1;\n        c01 += c0;\n        c101 += c10;\n      }\n    }\n    return c101 + c010;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;binary string&nbsp;s&nbsp;which represents the types of buildings along a street where: s[i] = &#8216;0&#8217;&nbsp;denotes that the&nbsp;ith&nbsp;building is an office and s[i] =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[20,8,18,177],"class_list":["post-9609","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-array","tag-counting","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9609","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=9609"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9609\/revisions"}],"predecessor-version":[{"id":9612,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9609\/revisions\/9612"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}