{"id":9462,"date":"2022-02-04T16:21:58","date_gmt":"2022-02-05T00:21:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9462"},"modified":"2022-02-04T16:22:43","modified_gmt":"2022-02-05T00:22:43","slug":"leetcode-2147-number-of-ways-to-divide-a-long-corridor","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2147-number-of-ways-to-divide-a-long-corridor\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2147. Number of Ways to Divide a Long Corridor"},"content":{"rendered":"\n<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>corridor<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;consisting of letters&nbsp;<code>'S'<\/code>&nbsp;and&nbsp;<code>'P'<\/code>&nbsp;where each&nbsp;<code>'S'<\/code>&nbsp;represents a seat and each&nbsp;<code>'P'<\/code>&nbsp;represents a plant.<\/p>\n\n\n\n<p>One room divider has&nbsp;<strong>already<\/strong>&nbsp;been installed to the left of index&nbsp;<code>0<\/code>, and&nbsp;<strong>another<\/strong>&nbsp;to the right of index&nbsp;<code>n - 1<\/code>. Additional room dividers can be installed. For each position between indices&nbsp;<code>i - 1<\/code>&nbsp;and&nbsp;<code>i<\/code>&nbsp;(<code>1 &lt;= i &lt;= n - 1<\/code>), at most one divider can be installed.<\/p>\n\n\n\n<p>Divide the corridor into non-overlapping sections, where each section has&nbsp;<strong>exactly two seats<\/strong>&nbsp;with any number of plants. There may be multiple ways to perform the division. Two ways are&nbsp;<strong>different<\/strong>&nbsp;if there is a position with a room divider installed in the first way but not in the second way.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of ways to divide the corridor<\/em>. Since the answer may be very large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>. If there is no way, return&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/04\/1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> corridor = \"SSPPSPS\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> There are 3 different ways to divide the corridor.\nThe black bars in the above image indicate the two room dividers already installed.\nNote that in each of the ways, <strong>each<\/strong> section has exactly <strong>two<\/strong> seats.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/04\/2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> corridor = \"PPSPSP\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> There is only 1 way to divide the corridor, by not installing any additional dividers.\nInstalling any would create some section that does not have exactly two seats.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/12\/3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> corridor = \"S\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There is no way to divide the corridor because there will always be a section that does not have exactly two seats.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == corridor.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>corridor[i]<\/code>&nbsp;is either&nbsp;<code>'S'<\/code>&nbsp;or&nbsp;<code>'P'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Combination<\/strong><\/h2>\n\n\n\n<p>If the 2k-th seat is positioned at j, and the 2k+1-th seat is at i. There are (i &#8211; j) ways to split between these two groups.<\/p>\n\n\n\n<p>ans = prod{i<sub>k<\/sub> &#8211; j<sub>k<\/sub>}<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberOfWays(string corridor) {\n    constexpr int kMod = 1e9 + 7;\n    long ans = 1;\n    long k = 0;\n    for (long i = 0, j = 0; i < corridor.size(); ++i) {\n      if (corridor[i] != 'S') continue;\n      if (++k > 2 && k & 1)\n        ans = ans * (i - j) % kMod;\n      j = i;\n    }\n    return (k >= 2 && k % 2 == 0) ? ans : 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a&nbsp;0-indexed&nbsp;string&nbsp;corridor&nbsp;of length&nbsp;n&nbsp;consisting of letters&nbsp;&#8216;S&#8217;&nbsp;and&nbsp;&#8216;P&#8217;&nbsp;where each&nbsp;&#8216;S&#8217;&nbsp;represents a seat and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[122,31],"class_list":["post-9462","post","type-post","status-publish","format-standard","hentry","category-math","tag-combination","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9462","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=9462"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9462\/revisions"}],"predecessor-version":[{"id":9465,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9462\/revisions\/9465"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}