id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfWays(string_view corridor) {\n constexpr int MOD = 1000000007;\n int seats = 0;\n uint_fast32_t res = 1;\n int mul = 1;\n for (const char &chr : corridor) {\n if (chr == 'S') {\n if (seats == 2) {\n res = (res * mul) % MOD;\n mul = 1;\n seats = 0;\n }\n seats++;\n } else if (seats == 2) {\n mul++;\n }\n }\n\n return (seats == 2) ? res : 0;\n }\n};", "memory": "28675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfWays(string corridor) {\n const int MOD = 1e9 + 7;\n long long result = 1;\n int seats = 0;\n int plants = 0;\n \n for (char c : corridor) {\n if (c == 'S') {\n if (seats == 2) {\n result = (result * (plants + 1)) % MOD;\n seats = 0;\n plants = 0;\n }\n seats++;\n } else if (seats == 2) {\n plants++;\n }\n }\n \n return (seats == 2) ? result : 0;\n }\n};", "memory": "28675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfWays(string corridor) {\n int MOD = 1e9 + 7, n = corridor.size(), ans = 1, n_plants = 0, idx = 0;\n\n // the first divider is already installed - simply skip the plants.\n while(idx < n and corridor[idx] != 'S') idx++;\n // we did not find any seat!\n if (idx == n) return 0;\n\n while (idx < n) {\n if (corridor[idx] == 'P') { n_plants++; idx++;}\n else {\n // we have encoutered one seat - consume plants till we find another seat.\n while(++idx < n and corridor[idx] != 'S');\n if (idx == n) return 0;\n ans = (ans * 1LL * (n_plants + 1)) % MOD;\n n_plants = 0;\n idx++;\n }\n }\n\n return ans;\n }\n};", "memory": "33425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfWays(string str) {\n int n = str.size() ;\n long long ans=1;\n\n long long last=-1;\n int count=0;\n for(int i=0;i<n;i++)\n {\n \n if(str[i]=='P')\n {\n continue;\n }\n count++;\n\n if(count>2 && count%2==1)\n {\n int b=(i-last);\n ans=(ans*b)%(1000000007);\n }\n\n last=i;\n \n }\n\n if(count==0 ||count%2==1)return 0;\n return ans;\n\n \n\n }\n};", "memory": "33425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
1
{ "code": "typedef long long ll;\nconst ll mod=1e9+7;\nclass Solution {\npublic:\n ll dp[100001][3];\n ll f(string &s,ll i,ll n,ll cnt){\n if(i==n && cnt==2)return 1;\n if(i==n)return 0;\n if(dp[i][cnt]!=-1)return dp[i][cnt]%mod;\n if(s[i]=='S' && cnt==2){\n return dp[i][cnt]=f(s,i+1,n,1)%mod;\n }\n else if(s[i]=='P' && cnt==2){\n return dp[i][cnt]=(f(s,i+1,n,0)%mod+f(s,i+1,n,cnt)%mod)%mod;\n }\n else{\n if(s[i]=='P')\n return dp[i][cnt]=f(s,i+1,n,cnt)%mod;\n else \n return dp[i][cnt]=f(s,i+1,n,cnt+1)%mod;\n }\n }\n int numberOfWays(string &s) {\n int n=s.length();\n memset(dp,-1,sizeof dp);\n return f(s,0,n,0)%mod;\n }\n};", "memory": "38175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfWays(string corridor) \n {\n string s=corridor;\n long res = 1, j = 0, k = 0, mod = 1e9 + 7;\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] == 'S') {\n if (++k > 2 && k % 2 == 1)\n res = res * (i - j) % mod;\n j = i;\n }\n }\n return k % 2 == 0 && k > 0 ? res : 0;\n }\n};", "memory": "38175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n // Store 1000000007 in a variable for convenience\n const int MOD = 1e9 + 7;\n \n // Count the number of ways to divide from \"index\" to the last index\n // with \"seats\" number of \"S\" in the current section\n int count(int index, int seats, string& corridor, int cache[][3]) {\n // If we have reached the end of the corridor, then\n // the current section is valid only if \"seats\" is 2\n if (index == corridor.length()) {\n return seats == 2 ? 1 : 0;\n }\n\n // If we have already computed the result of this sub-problem,\n // then return the cached result\n if (cache[index][seats] != -1) {\n return cache[index][seats];\n }\n\n // Result of the sub-problem\n int result = 0;\n\n // If the current section has exactly 2 \"S\"\n if (seats == 2) {\n // If the current element is \"S\", then we have to close the\n // section and start a new section from this index. Next index\n // will have one \"S\" in the current section\n if (corridor[index] == 'S') {\n result = count(index + 1, 1, corridor, cache);\n } else {\n // If the current element is \"P\", then we have two options\n // 1. Close the section and start a new section from this index\n // 2. Keep growing the section\n result = (count(index + 1, 0, corridor, cache) + count(index + 1, 2, corridor, cache)) % MOD; \n }\n } else {\n // Keep growing the section. Increment \"seats\" if present\n // element is \"S\"\n if (corridor[index] == 'S') {\n result = count(index + 1, seats + 1, corridor, cache);\n } else {\n result = count(index + 1, seats, corridor, cache);\n }\n }\n\n // Memoize the result, and return it\n cache[index][seats] = result;\n return cache[index][seats];\n }\n\n int numberOfWays(string corridor) {\n // Cache the result of each sub-problem\n int cache[corridor.length()][3];\n memset(cache, -1, sizeof(cache));\n\n // Call the count function\n return count(0, 0, corridor, cache);\n }\n};", "memory": "42925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n unordered_map<string, int> dp;\n int c = 1000000007;\n int util(string &corridor , int start, int chairs, int n, int cache[][3]){\n if(start == n){\n return chairs == 2 ? 1 : 0;\n }\n\n if(cache[start][chairs] != -1){\n return cache[start][chairs];\n }\n int result = 0;\n if(chairs == 2){\n if(corridor[start] == 'S'){\n result = util(corridor, start+1, 1, n, cache)%c;\n }else{\n result = (util(corridor, start+1, 0, n,cache) + util(corridor, start+1, 2, n,cache))%c;\n }\n }else{\n if(corridor[start] == 'S'){\n result = util(corridor, start+1, chairs+1, n, cache)%c;\n }else{\n result = util(corridor, start+1, chairs, n, cache)%c;\n }\n }\n cache[start][chairs] = result;\n return cache[start][chairs];\n }\n int numberOfWays(string corridor) {\n int cache[corridor.length()][3];\n memset(cache, -1, sizeof(cache));\n return util(corridor, 0, 0, corridor.size(),cache);\n }\n};", "memory": "42925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfWays(string corridor) {\n std::uint64_t ways = 1;\n std::vector<int> gaps;\n int start = -1;\n int seats = 0;\n for (std::size_t i = 0; i < std::size(corridor); ++i) {\n if (corridor[i] == 'S') {\n ++seats;\n if (seats % 2 == 1) {\n if (start >= 0) {\n gaps.push_back(i - start);\n }\n } else {\n start = i;\n }\n }\n }\n if (seats < 2 || seats % 2 == 1) {\n return 0;\n }\n static std::uint64_t constexpr M = 1'000'000'000 + 7;\n for (auto const& gap : gaps) ways = (ways * gap) % M;\n return ways;\n }\n};", "memory": "47675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
2
{ "code": "const int MOD = 1e9+7;\nconst int MAX = 1e5+5;\nclass Solution {\npublic:\n string corr;\n int n;\n int dp[MAX][4];\n int solve(int i,int c){\n if(i==n){\n return c==2;\n }\n if(~dp[i][c]) return dp[i][c];\n c+=(corr[i]=='S');\n if(c<2) return dp[i][c]=solve(i+1,c);\n else if(c==2) return dp[i][c]=(solve(i+1,0)+solve(i+1,c))%MOD;\n else return dp[i][c]=0;\n\n }\n int numberOfWays(string corridor) {\n memset(&dp,-1,sizeof(dp));\n corr=corridor;\n n=corr.size();\n return solve(0,0);\n }\n};", "memory": "47675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\n public:\n auto numberOfWays(const std::string& corridor) -> int {\n std::vector<int> pos;\n for (int i = 0; i < static_cast<int>(corridor.size()); ++i) {\n if (corridor[i] == 'S') {\n pos.push_back(i);\n }\n }\n if (pos.size() % 2 == 1 || pos.size() == 0) {\n return 0;\n }\n long long res = 1;\n int prev = pos[1];\n for (int i = 2; i < static_cast<int>(pos.size()); i += 2) {\n res = (res * (pos[i] - prev)) % 1000000007;\n prev = pos[i + 1];\n }\n return res;\n }\n};", "memory": "52425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int help(int i,string &s,vector<int> &dp){\n if(i>=s.length()) {\n return 1;\n }\n if(dp[i]!=-1) return dp[i];\n int seat=0;\n long long ans=0;\n for(int ind=i;ind<s.length();ind++){\n if(s[ind]=='S'){\n seat++;\n }\n if(seat==2){\n ans+=(help(ind+1,s,dp)%mod);\n }\n }\n return dp[i]=ans%mod;\n }\n int numberOfWays(string corridor) {\n int seat=0;\n int i=0,n=corridor.length();\n while(i<=n-1){\n if(corridor[i]=='S') seat++;\n i++;\n }if(seat==0) return 0;\n if(seat==4 && corridor[0]=='S' && corridor[1]=='S' && corridor[n-2]=='S' && corridor[n-1]=='S') return n-3;\n if(seat%2 == 0){\n vector<int> dp(corridor.size()+1,0);\n dp[n]=1;\n for(int i=n-1;i>=0;i--){\n int seat=0;\n long long ans=0;\n for(int ind=i;ind<n;ind++){\n if(corridor[ind]=='S'){\n seat++;\n }\n if(seat==2){\n ans+=(dp[ind+1]%mod);\n }if(seat>2) break;\n }\n dp[i]=ans%mod;\n }\n // return help(0,corridor,dp);\n return dp[0];\n }\n return 0;\n }\n};", "memory": "52425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
2
{ "code": "int mod=1e9+7;\nint solve(int i,vector<int>&v,string &s,vector<int>&dp)\n{\n if(i==v.size()-1)\n {\n return 1;\n }\n if(dp[i]!=-1)\n return dp[i];\n long long ways=(solve(i+1,v,s,dp)%mod);\n for(int k=v[i]+1;k<s.size();k++)\n {\n if(s[k]=='S')\n break;\n ways=((ways%mod)+(solve(i+1,v,s,dp)%mod))%mod;\n\n }\n\n return dp[i]=ways;\n}\n\nclass Solution {\npublic:\n int numberOfWays(string corridor) {\n vector<int>v;\n int count=0;\n for(int i=0;i<corridor.size();i++)\n {\n if(corridor[i]=='S')\n count++;\n\n if(count==2)\n {\n count=0;\n v.push_back(i);\n }\n\n \n }\n if(count%2||v.size()==0)\n return 0;\n vector<int>dp(v.size()+10,-1);\n return solve(0,v,corridor,dp);\n\n\n \n }\n};", "memory": "57175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n \n int numberOfWays(string corridor) {\n vector<int> pos_seats;\n \n for (int i = 0; i < corridor.size(); i++) {\n if (corridor[i] == 'S') {\n pos_seats.push_back(i);\n }\n }\n \n if (pos_seats.size() % 2 || pos_seats.size() == 0)\n return 0;\n \n long long result = 1;\n int prev = pos_seats[1]; \n \n for (int i = 2; i < pos_seats.size(); i += 2) {\n int length = pos_seats[i] - prev;\n result = (result * length) % mod;\n \n prev = pos_seats[i + 1];\n }\n \n return result;\n }\n};\n", "memory": "57175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfWays(string s) {\n vector<int> numbers ;\n int mod = 1e9 + 7 ; \n int temp = 0 ; \n int plant = 0 ; \n for(int i=0;i<s.length();i++){ \n if(s[i] == 'S'){\n numbers.push_back(i) ;\n }\n }\n int check = 0 ; \n for(int i=0;i<s.length();i++){\n if(s[i] == 'S') check++ ; \n }\n if(check % 2 != 0) return 0 ; \n if(numbers.size() <= 1) return 0 ; \n vector<int> ans ; \n for(int i=0;i<numbers.size()-1;i++){\n if(i%2 == 1) {\n ans.push_back(numbers[i+1]-numbers[i]) ; \n }\n }\n long long t = 1 ; \n for(int i=0;i<ans.size();i++){\n t = t * ans[i] ; \n t = t% mod ; \n }\n return t ; \n }\n};", "memory": "61925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "#include <iostream>\n#include <vector>\n#include <string>\n#include <sstream>\n#include <set>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <cmath>\n#include <cctype>\n#include <iomanip>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\n\nconst ll MOD = 1e9 + 7;\nclass Solution {\npublic:\n int numberOfWays(string corridor) {\n \tint n = corridor.size();\n \tstring s = \"\";\n \tfor (auto &ch : corridor) {\n \t\ts.push_back(ch);\n \t\ts.push_back('x');\n \t}\n \tint total = 0;\n \tvector<int> groups;\n \tint m = s.size();\n \tint run = 0;\n \tfor (int i = 0; i < m; i++) {\n \t\tif (s[i] == 'x' && !(total%2) && total) {\n \t\t\trun += 1;\n \t\t} else if (s[i] == 'S') {\n \t\t\ttotal += 1;\n \t\t\tif (!(total%2) && total > 2) {\n \t\t\t\tif (total > 2) {\n \t\t\t\t\tgroups.push_back(run);\n \t\t\t\t}\n \t\t\t\trun = 0;\n \t\t\t}\n \t\t}\n \t}\n \tint ans = 1;\n \tfor (auto &i : groups) {\n \t\tans = ((ans%MOD)*(i%MOD))%MOD;\n \t}\n \tif (!total) {\n \t\treturn 0; // empty\n \t}\n \tif (total%2) {\n \t\treturn 0; // odd number\n \t}\n \tif (!groups.size()) {\n \t\treturn 1;\n \t}\n \treturn ans;\n }\n};\t", "memory": "66675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "/*\nclass Solution {\n int mod = 1e9+7;\n int solve(int i, int j , string &corridor,int count , vector<int> &dp){\n int n = corridor.size();\n if (i>j) {\n return 1;\n }\n if(dp[i]!= -1){\n return dp[i];\n }\n int ans = 0;\n count = 0; // To count 'S' characters\n for (int k = i; k <= j; k++) {\n if (corridor[k] == 'S') count++;\n if (count == 2) {\n ans = (ans + solve(k + 1, j, corridor,count, dp) % mod) % mod;\n }\n else if(count>2)\n return 0;\n }\n return dp[i]= ans%mod;\n }\npublic:\n int numberOfWays(string corridor) {\n int n = corridor.size();\n int count = 0;\n for (char c : corridor) {\n if (c == 'S') count++;\n }\n if (count < 2 || count % 2 != 0) return 0; // Edge case handling\n vector<int> dp(n, -1);\n return solve(0,n-1,corridor,0,dp);\n }\n}; */\n\n/*At First sight you may think like i will choose where to partion and use front partition DP and blah.. blah.. blah...\n\nBut hold on dear,\nhave you looked at constraints ?\nit will cost you o(n^2) and n is about 1e5 , then how it will work ?\n\nfor front partition DP , your n should be about 1e3.\nit will be solved by greedy */\nclass Solution {\npublic:\n\n \n int numberOfWays(string cr) {\n\n int n = cr.size();\n int md = 1e9 + 7; \n int cnt = 0; \n int temp = 1; \n\n vector < int > arr;\n for(int i = 0 ; i < cr.size() ; i++){\n if(cr[i] == 'S') cnt++; \n cnt %=2; \n if(cnt == 0 && cr[i] == 'P') temp++; \n else{\n arr.push_back(temp); \n temp = 1; \n }\n }\n\n long long ans = 1; \n if(arr.size() <= 1 || cnt == 1) return 0; \n for(int i = 1 ; i < arr.size() ; i++){\n ans= ( ans % md * arr[i] % md) % md; \n }\n return ans; \n\n\n }\n};", "memory": "71425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfWays(string corridor) {\n int n=corridor.length();\n vector<int>prefixCnt(n,0);\n prefixCnt[0]=(corridor[0]=='S')?1:0;\n for(int i=1;i<n;i++){\n if(corridor[i]=='S'){\n prefixCnt[i]=prefixCnt[i-1]+1;\n }\n else{\n prefixCnt[i]=prefixCnt[i-1];\n }\n }\n if(prefixCnt[n-1]%2!=0) return 0;\n vector<long>cnt(prefixCnt[n-1]+1,0);\n for(int i=0;i<n;i++){\n cnt[prefixCnt[i]]++;\n }\n // for(int i=0;i<(prefixCnt[n-1]+1);i++){\n // cout<<cnt[i]<<\" \";\n // }\n long mod=1000000007;\n long ans=1;\n long mx=prefixCnt[n-1]-2;\n for(int i=2;i<=mx;i+=2){\n ans=(ans%mod*cnt[i]%mod)%mod;\n }\n if(prefixCnt[n-1]==0) return 0;\n return (int)ans;\n }\n};", "memory": "76175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfWays(string corridor) {\n int n = corridor.length();\n vector<int> prefixCnt(n, 0);\n prefixCnt[0] = (corridor[0] == 'S') ? 1 : 0;\n for (int i = 1; i < n; i++) {\n if (corridor[i] == 'S') {\n prefixCnt[i] = prefixCnt[i - 1] + 1;\n } else {\n prefixCnt[i] = prefixCnt[i - 1];\n }\n }\n if (prefixCnt[n - 1] % 2 != 0)\n return 0;\n vector<long> cnt(prefixCnt[n - 1] + 1, 0);\n for (int i = 0; i < n; i++) {\n cnt[prefixCnt[i]]++;\n }\n long mod = 1000000007;\n long ans = 1;\n long mx = prefixCnt[n - 1] - 2;\n for (int i = 2; i <= mx; i += 2) {\n ans = (ans % mod * cnt[i] % mod) % mod;\n }\n if (prefixCnt[n - 1] == 0)\n return 0;\n return (int)ans;\n }\n};", "memory": "76175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "#define ll long long\n#define MOD 1000000007\nclass Solution {\npublic:\n int numberOfWays(string corridor) {\n ll n = corridor.size();\n vector<ll> seat_idx;\n ll seatCount = 0;\n for(ll i = 0; i<corridor.size(); ++i) {\n if(corridor[i] == 'S') {\n seatCount++;\n seat_idx.push_back(i);\n }\n }\n if(seatCount%2 || seatCount == 0) return 0;\n ll ans = 1;\n for(ll i = 2; i<seat_idx.size(); i+=2) {\n ans *= (seat_idx[i] - seat_idx[i-1])%MOD;\n ans %= MOD;\n }\n return (int)ans;\n }\n\n};", "memory": "80925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "#define ll long long\n#define M 1000000007\n\nclass Solution {\n\n ll multiply(ll a, ll b) {\n return ((a*b)%M +M)%M;\n }\n\npublic:\n int numberOfWays(string corridor) {\n while (!corridor.empty() && corridor.back() == 'P') corridor.pop_back();\n reverse(corridor.begin(), corridor.end());\n while (!corridor.empty() && corridor.back() == 'P') corridor.pop_back();\n vector<ll> seat;\n for (int i = 0; i<corridor.size(); i++) if (corridor[i] == 'S') seat.push_back(i);\n if (seat.size()==0 || seat.size()%2 == 1) return 0;\n ll ans = 1;\n for (int i=2; i<seat.size(); i+=2) {\n ll shortAns = seat[i]-seat[i-1];\n ans = multiply(ans, shortAns);\n }\n return ans;\n }\n};", "memory": "80925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int MOD = 1e9 + 7;\n\n void precomputeNextIdx(const vector<int>& pref, vector<int>& next_idx) {\n int n = pref.size();\n for (int i = 0; i < n; ++i) {\n int limit = (i > 0) ? pref[i - 1] + 2 : 2;\n // Binary search for the first index with prefix sum >= limit\n auto it = lower_bound(pref.begin(), pref.end(), limit);\n next_idx[i] = (it != pref.end()) ? it - pref.begin() : n;\n }\n }\n\n int solve(string &s, vector<int>& dp, int i, const vector<int>& next_idx, const vector<int>& pref) {\n int n = s.size();\n if (i == n) return 1;\n\n if (dp[i] != -1) return dp[i];\n\n int ans = 0;\n int idx = next_idx[i];\n\n for (int j = idx; j < n; ++j) {\n // int r = pref[j];\n // int l = (i > 0) ? pref[i - 1] : 0;\n // if (r - l > 2) {\n // break;\n // }\n if(j!=idx&&s[j]=='S') break;\n \n ans = (ans + solve(s, dp, j + 1, next_idx, pref)) % MOD;\n \n }\n\n dp[i] = ans;\n return ans;\n }\n\n int numberOfWays(string corridor) {\n int n = corridor.size();\n vector<int> dp(n, -1); // Initialize DP vector\n\n vector<int> pref(n, 0);\n if (corridor[0] == 'S') pref[0] = 1;\n for (int i = 1; i < n; i++) {\n pref[i] = pref[i - 1];\n if (corridor[i] == 'S') pref[i]++;\n }\n\n vector<int> next_idx(n, n); // Initialize next_idx array\n precomputeNextIdx(pref, next_idx);\n\n return solve(corridor, dp, 0, next_idx, pref);\n }\n};\n", "memory": "85675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int MOD = 1e9 + 7;\n\n void precomputeNextIdx(const vector<int>& pref, vector<int>& next_idx) {\n int n = pref.size();\n for (int i = 0; i < n; ++i) {\n int limit = (i > 0) ? pref[i - 1] + 2 : 2;\n // Binary search for the first index with prefix sum >= limit\n auto it = lower_bound(pref.begin(), pref.end(), limit);\n next_idx[i] = (it != pref.end()) ? it - pref.begin() : n;\n }\n }\n\n int solve(string &s, vector<int>& dp, int i, const vector<int>& next_idx) {\n int n = s.size();\n if (i == n) return 1;\n\n if (dp[i] != -1) return dp[i];\n\n int ans = 0;\n int idx = next_idx[i];\n\n for (int j = idx; j < n; ++j) {\n \n if(j!=idx&&s[j]=='S') break;\n \n ans = (ans + solve(s, dp, j + 1, next_idx)) % MOD;\n \n }\n\n dp[i] = ans;\n return ans;\n }\n\n int numberOfWays(string corridor) {\n int n = corridor.size();\n vector<int> dp(n, -1); // Initialize DP vector\n\n vector<int> pref(n, 0);\n if (corridor[0] == 'S') pref[0] = 1;\n for (int i = 1; i < n; i++) {\n pref[i] = pref[i - 1];\n if (corridor[i] == 'S') pref[i]++;\n }\n\n vector<int> next_idx(n, n); // Initialize next_idx array\n precomputeNextIdx(pref, next_idx);\n\n return solve(corridor, dp, 0, next_idx);\n }\n};\n", "memory": "90425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n vector<vector<int>> dp;\n int Mod = 1e9+7;\npublic:\n int Memo(string& c,int index,int cnt){\n if(index==c.size() && cnt==0) return 1;\n if(index>=c.size()) return 0;\n if(dp[cnt][index]!=-1) return dp[cnt][index];\n if(cnt!=0){\n if(c[index]=='S') return dp[cnt][index] = (Memo(c,index+1,cnt-1))%Mod;\n return dp[cnt][index] = (Memo(c,index+1,cnt))%Mod;\n }\n if(c[index]=='S') return dp[cnt][index] = (Memo(c,index+1,1))%Mod;\n return dp[cnt][index] = (Memo(c,index+1,cnt)%Mod + Memo(c,index+1,2)%Mod)%Mod;\n }\n int numberOfWays(string corridor) {\n dp.resize(3,vector<int>(corridor.size(),-1));\n return Memo(corridor,0,2);\n }\n};", "memory": "95175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int f(int i, string& corridor, int seats, vector<vector<int>>& dp)\n {\n int n=corridor.length();\n if(i>=n)\n {\n if(seats==2)return 1;\n return 0;\n }\n if(dp[seats][i]!=-1)return dp[seats][i];\n int ways=0;\n if(corridor[i]=='S')\n {\n if(seats==2)ways+=f(i+1,corridor,1,dp);\n else ways+=f(i+1,corridor,seats+1,dp);\n }\n else\n {\n if(seats==2)\n {\n ways+=f(i+1,corridor,seats,dp);\n ways+=f(i+1,corridor,0,dp);\n }\n else ways+=f(i+1,corridor,seats,dp);\n }\n return dp[seats][i]=ways%mod;\n }\n int numberOfWays(string corridor) {\n int n=corridor.length();\n vector<vector<int>>dp(3,vector<int>(n,-1));\n return f(0,corridor,0, dp)%mod;\n }\n};", "memory": "99925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\nint MOD=1e9+7;\n int numberOfWays(string corridor) {\n int f=0;\n for(auto ch:corridor){\n if(ch=='S') f++;\n }\n if(f%2 or f==0) return 0;\n vector<vector<int>> rec;\n int n=corridor.size();\n int i=0;\n while(i<n){\n if(corridor[i]=='S'){\n int j=i; int cnt=0;\n while(j<n and cnt<2){\n if(corridor[j]=='S') cnt++;\n j++;\n }\n if(cnt==2) rec.push_back({i,j-1});\n i=j;\n }\n else{\n i++;\n }\n }\n if(rec.size()==0) return 0;\n if(rec.size()==1) return 1;\n long long ans=1;\n for(int i=1;i<rec.size();i++){\n long long k=(rec[i][0]-rec[i-1][1]);\n ans=(ans*(k))%MOD; \n }\n return ans;\n }\n};", "memory": "104675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfWays(string s) {\n int n = s.length();\n int totalS = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 'S') {\n totalS++;\n }\n }\n if (totalS == 2) {\n return 1;\n }\n \n if (totalS == 0 || totalS % 2 != 0) {\n return 0;\n }\n\n long long ans = 1;\n int t = 0;\n int prev = 0; \n unordered_map<int,int> mp;\n for(int i=n-1;i>=0;i--){\n if(s[i] == 'S'){\n t++;\n }\n if(t==2){\n prev = i;\n t=0;\n }\n else if(t==1){\n mp[i] = prev;\n }\n }\n\n int MOD = 1e9 + 7;\n t = 0;\n int k = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 'S') {\n t++;\n k++;\n }\n \n if (t == 2) {\n ans = (ans * (mp[i] - i)) % MOD;\n cout<<i<<\" \";\n t = 0;\n // cout << mp[i] << \" \"; // For debugging purposes\n }\n if (totalS - k == 2) {\n break;\n }\n }\n\n return ans;\n }\n};\n", "memory": "109425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "\n#pragma GCC optimize(\"Ofast\",\"inline\",\"fast-math\",\"unroll-loops\",\"no-stack-protector\")\n\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\",\"f16c\")\n\nstatic const auto fast = []() {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); return 0; } ();\n\nclass Solution {\npublic:\n int numberOfWays(string s) {\n int n = s.length();\n int totalS = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 'S') {\n totalS++;\n }\n }\n if (totalS == 2) {\n return 1;\n }\n \n if (totalS == 0 || totalS % 2 != 0) {\n return 0;\n }\n\n long long ans = 1;\n int t = 0;\n int prev = 0; \n unordered_map<int,int> mp;\n for(int i=n-1;i>=0;i--){\n if(s[i] == 'S'){\n t++;\n }\n if(t==2){\n prev = i;\n t=0;\n }\n else if(t==1){\n mp[i] = prev;\n }\n }\n\n int MOD = 1e9 + 7;\n t = 0;\n int k = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 'S') {\n t++;\n k++;\n }\n \n if (t == 2) {\n ans = (ans * (mp[i] - i)) % MOD;\n cout<<i<<\" \";\n t = 0;\n // cout << mp[i] << \" \"; // For debugging purposes\n }\n if (totalS - k == 2) {\n break;\n }\n }\n\n return ans;\n }\n};\n", "memory": "114175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int MOD = 1e9+7;\n int numberOfWays(string corridor) {\n int n = corridor.size();\n vector<vector<int>>dp(n + 1, vector<int>(3));\n\n dp[n][0] = 0;\n dp[n][1] = 0;\n dp[n][2] = 1;\n\n for(int i = n-1; i>=0; i--) {\n if(corridor[i] == 'S') {\n dp[i][0] = dp[i+1][1];\n dp[i][1] = dp[i+1][2];\n dp[i][2] = dp[i+1][1];\n } else {\n dp[i][0] = dp[i+1][0];\n dp[i][1] = dp[i+1][1];\n dp[i][2] = (dp[i+1][0] + dp[i+1][2])%MOD;\n }\n }\n return dp[0][0];\n // return dfs(corridor, 0, 0, dp);\n }\n\n int dfs(string& s, int pos, int k, vector<vector<int>>&dp) {\n if(pos >= s.length()) return k == 2;\n if(dp[pos][k] != -1) return dp[pos][k];\n if(k == 2) {\n if(s[pos] == 'P') return dp[pos][k] = (dfs(s, pos+1, k, dp) + dfs(s, pos+1, 0, dp))%MOD;\n else return dp[pos][k] = dfs(s, pos+1, 1, dp);\n } else {\n return dp[pos][k] = dfs(s, pos+1, k + (s[pos] == 'S'), dp);\n }\n }\n};", "memory": "137925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n// int dp[100001][3];\n// int f(int i,int c,string corridor,int n){\n// if(i>=n){\n// if(c==2) return 1;\n// else return 0;\n// }\n// if(dp[i][c]!=-1){\n// return dp[i][c];\n// }\n// if(c==0){\n// if(corridor[i]=='S'){\n// return dp[i][c]=f(i+1,c+1,corridor,n)%((int)(1e9+7));\n// }\n// else return dp[i][c]=f(i+1,c,corridor,n)%((int)(1e9+7));\n// }\n// else if(c==1){\n// if(corridor[i]=='S'){\n// return dp[i][c]=f(i+1,c+1,corridor,n)%((int)(1e9+7));\n// }\n// else return dp[i][c]=f(i+1,c,corridor,n)%((int)(1e9+7));\n// }\n// if(corridor[i]=='S'){\n// return dp[i][c]=f(i+1,1,corridor,n)%((int)(1e9+7));\n// }\n// return dp[i][c]=(f(i+1,c,corridor,n)+f(i+1,0,corridor,n))%((int)(1e9+7));\n// }\n int numberOfWays(string corridor) {\n int n=corridor.size();\n vector<vector<int>> dp(n+1,vector<int>(3,0));\n dp[n][2]=1;\n for(int i=n-1;i>=0;i--){\n for(int j=2;j>=0;j--){\n if(j==0){\n if(corridor[i]=='S'){\n dp[i][j]=dp[i+1][j+1];\n }\n else dp[i][j]=dp[i+1][j];\n }\n else if(j==1){\n if(corridor[i]=='S'){\n dp[i][j]=dp[i+1][j+1];\n }\n else dp[i][j]=dp[i+1][j];\n }\n else{\n if(corridor[i]=='S'){\n dp[i][j]=dp[i+1][1];\n }\n else{\n dp[i][j]=(dp[i+1][j]+dp[i+1][0])%((int)(1e9+7));\n }\n }\n }\n }\n return dp[0][0];\n }\n};", "memory": "142675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod = 1e9+7;\n\n int numberOfWays(string s) {\n\n int n = s.size();\n\n vector<vector<int>> dp(n+1, vector<int>(3, 0));\n dp[n][2] = 1;\n\n for(int idx=n-1; idx>=0; idx--) {\n for(int cnt=2; cnt>=0; cnt--) {\n \n int ans = 0;\n\n if(s[idx] == 'S') {\n if(cnt+1 <= 2) ans = dp[idx+1][cnt+1];\n if(cnt == 2) ans = (ans + dp[idx+1][1])%mod;\n } \n else {\n ans = dp[idx+1][cnt];\n if(cnt == 2) ans = (ans + dp[idx+1][0])%mod;\n }\n\n dp[idx][cnt] = ans;\n }\n }\n \n return dp[0][0];\n }\n};", "memory": "147425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\nint M = 1e9+7;\n int f(int ind, int seats, string &s, vector<vector<int>> &dp){\n if(ind == s.size()){\n if(seats==2) return 1;\n else return 0;\n }\n if(dp[ind][seats]!=-1) return dp[ind][seats];\n int ways1 = 0, ways2 = 0;\n if(seats == 2){\n if(s[ind]=='S'){\n ways1 = f(ind+1, 1, s, dp);\n }\n else{\n ways2 = (f(ind+1, 0, s, dp) + f(ind+1, 2, s, dp))%M;\n }\n }\n else{\n if(s[ind] == 'S'){\n ways1 =f(ind+1, seats+1, s, dp);\n }\n else{\n ways2 = f(ind+1, seats ,s ,dp);\n }\n }\n return dp[ind][seats] = ways1 + ways2;\n }\n int numberOfWays(string s) {\n int n = s.size();\n vector<vector<int>> dp(n+2, vector<int> (4,0));\n dp[n][2] = 1;\n\n for(int ind=n-1; ind>=0; ind--){\n for(int seats=0; seats<=2; seats++){\n int ways1 = 0, ways2 = 0;\n if(seats == 2){\n if(s[ind]=='S'){\n ways1 = dp[ind+1][1];\n }\n else{\n ways2 = (dp[ind+1][0] + dp[ind+1][2])%M;\n }\n }\n else{\n if(s[ind] == 'S'){\n ways1 = dp[ind+1][seats+1];\n }\n else{\n ways2 = dp[ind+1][seats];\n }\n }\n dp[ind][seats] = ways1 + ways2;\n }\n }\n\n return dp[0][0]; //f(ind, seats)\n }\n};", "memory": "152175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "#define a 1000000007\nclass Solution {\npublic:\n\n int recursive(string& corridor, int ind, int count,vector<vector<int>>& dp){\n if(ind==corridor.size()-1){ \n if(corridor[ind]=='S') count++;\n if(count==2) return 1;\n return 0;\n }\n\n if(dp[ind][count]!=-1) return dp[ind][count]%a;\n if(corridor[ind]=='S'){\n \n if(count>=2) return 0;\n if(count==1) return dp[ind][count]=(recursive(corridor, ind+1, count+1, dp)%a + recursive(corridor, ind+1, 0, dp)%a)%a;\n else return dp[ind][count]=recursive(corridor, ind+1, count+1, dp)%a;\n }\n else{\n if(count>2) return 0;\n if(count==2) return dp[ind][count]=(recursive(corridor, ind+1, count, dp)%a + recursive(corridor, ind+1, 0, dp)%a)%a;\n else return dp[ind][count]=recursive(corridor, ind+1, count, dp)%a;\n }\n\n \n }\n int numberOfWays(string corridor) {\n int counts=0;\n for(auto v: corridor){\n if(v=='S') counts++;\n }\n if(counts<2) return 0;\n if(counts==2) return 1;\n vector<vector<int>> dp(corridor.length(),vector<int>(3,-1));\n return recursive(corridor,0, 0, dp);\n }\n};", "memory": "156925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n vector<vector<int>> dp;\n int solve(string& s,int i,int currSeat){\n if(i==s.size()){\n return currSeat==2?1:0;\n }\n if(dp[i][currSeat]!=-1)return dp[i][currSeat];\n int res=0;\n if(currSeat==2){\n if(s[i]=='P'){\n int putWall=solve(s,i+1,0);\n int notPutWall=solve(s,i+1,2);\n res=(putWall+notPutWall)%mod;\n }\n else res=solve(s,i+1,1);\n }\n else{\n if(s[i]=='S') res=solve(s,i+1,currSeat+1);\n else res=solve(s,i+1,currSeat);\n }\n return dp[i][currSeat]=res;\n }\n int numberOfWays(string& corridor) {\n dp.resize(corridor.size(),vector<int>(3,-1));\n return solve(corridor,0,0);\n }\n};", "memory": "161675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int mod = 1e9+7;\n\n vector<vector<int>> memo;\n\n int solve(string& s, int idx, int cnt) {\n\n if(idx == n) return (cnt == 2);\n\n if(memo[idx][cnt] != -1) return memo[idx][cnt];\n\n int ans = 0;\n\n if(cnt == 2) {\n if(s[idx] == 'S') ans = solve(s, idx+1, 1);\n else {\n int take = solve(s, idx+1, cnt);\n int nottake = solve(s, idx+1, 0);\n ans = (take+nottake)%mod;\n }\n }\n else {\n if(s[idx] == 'S') cnt++;\n ans = solve(s, idx+1, cnt);\n }\n\n return memo[idx][cnt] = ans%mod;\n }\n\n int numberOfWays(string s) {\n \n n = s.size();\n memo.resize(n, vector<int>(3, -1));\n return solve(s, 0, 0); \n }\n};", "memory": "166425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\n int mod;\n int helper(string& corridor, int idx, int cnt, vector<vector<int>>& dp) {\n int ans = 0;\n\n if (corridor.length() == idx) {\n if (cnt == 2)\n return 1;\n return 0;\n }\n\n if (dp[idx][cnt] != -1)\n return dp[idx][cnt];\n\n if (corridor[idx] == 'S')\n cnt++;\n if (cnt > 2)\n return 0;\n if (cnt == 2) {\n ans = (ans+helper(corridor, idx + 1, 0, dp))%(mod);\n }\n if (cnt <= 2) {\n ans = (ans+helper(corridor, idx + 1, cnt, dp))%(mod);\n }\n dp[idx][cnt] = ans;\n return ans;\n }\n\npublic:\n int numberOfWays(string corridor) {\n int n = corridor.length();\n mod = 1e9+7;\n vector<vector<int>> dp(n, vector<int>(3, -1));\n return helper(corridor, 0, 0, dp);\n }\n};", "memory": "166425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int mod = 1e9+7;\n\n vector<vector<int>> memo;\n\n int solve(string& s, int idx, int cnt) {\n\n if(idx == n) return (cnt == 2);\n\n if(memo[idx][cnt] != -1) return memo[idx][cnt];\n\n int ans = 0;\n\n if(cnt == 2) {\n if(s[idx] == 'S') ans = solve(s, idx+1, 1);\n else {\n int take = solve(s, idx+1, cnt);\n int nottake = solve(s, idx+1, 0);\n ans = (take+nottake)%mod;\n }\n }\n else {\n if(s[idx] == 'S') cnt++;\n ans = solve(s, idx+1, cnt);\n }\n\n return memo[idx][cnt] = ans%mod;\n }\n\n int numberOfWays(string s) {\n \n n = s.size();\n memo.resize(n, vector<int>(3, -1));\n return solve(s, 0, 0); \n }\n};", "memory": "171175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int mod = 1e9+7;\n\n vector<vector<int>> memo;\n\n int solve(string& s, int idx, int cnt) {\n\n if(idx == n) return (cnt == 2);\n\n if(memo[idx][cnt] != -1) return memo[idx][cnt];\n\n int ans = 0;\n\n if(cnt == 2) {\n if(s[idx] == 'S') ans = solve(s, idx+1, 1);\n else {\n int take = solve(s, idx+1, cnt);\n int nottake = solve(s, idx+1, 0);\n ans = (take+nottake)%mod;\n }\n }\n else {\n if(s[idx] == 'S') cnt++;\n ans = solve(s, idx+1, cnt);\n }\n\n return memo[idx][cnt] = ans%mod;\n }\n\n int numberOfWays(string s) {\n \n n = s.size();\n memo.resize(n, vector<int>(3, -1));\n return solve(s, 0, 0); \n }\n};", "memory": "171175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int solve(int index,int count,string &s,vector<vector<int>>&dp){\n // base case\n if(index>=s.size()){\n if(count==2)\n return 1;\n else\n return 0;\n }\n if(count>2)\n return 0;\n if(dp[index][count]!=-1)\n return dp[index][count];\n if(s[index]=='S')\n count++;\n long long int ans=0;\n ans+=solve(index+1,count,s,dp);\n if(count==2){\n ans+=solve(index+1,0,s,dp);\n }\n return dp[index][count]=ans%mod;\n }\n int numberOfWays(string s) {\n int n=s.size();\n vector<vector<int>>dp(n+1,vector<int>(4,-1));\n int ans=solve(0,0,s,dp);\n return ans;\n }\n};", "memory": "175925" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n const int mod = 1e9+7;\n int numberOfWays(string str) {\n dp.resize(str.length(),vector<int>(4,-1));\n int idx = 0;\n int count = 0;\n return fun(idx,str,count)%mod;\n }\n long long int fun(int idx , string &str , int count){\n\n if(idx >= str.length() and (count == 2)) return 1;\n if(idx >= str.length()) return 0;\n if(count > 2) return 0;\n\n if(dp[idx][count]!=-1) return dp[idx][count]%mod;\n\n if(str[idx] == 'S')\n {\n if((count+1) == 2)\n {\n long long int choise1 = fun(idx+1,str,0); //partition choise taken\n long long int choise2 = fun(idx+1,str,count+1); //partition choise ignored\n return dp[idx][count] = 0LL + (choise1%mod + choise2%mod)%mod;\n } \n else if((count + 1) < 2)\n {\n long long int choise1 = fun(idx+1,str,count+1); //partition choise ignored\n return dp[idx][count] = 0LL + choise1 % mod;\n } \n else if((count + 1) > 2) return dp[idx][count] = 0; //more seats than 2 are not allowed\n }\n else if(str[idx] == 'P')\n {\n if(count == 2){\n long long int choise1 = fun(idx+1,str,0);\n long long int choise2 = fun(idx+1,str,count);\n return dp[idx][count] = 0LL + (choise1%mod + choise2%mod)%mod;\n }\n else if(count < 2){\n long long int choise1 = fun(idx+1,str,count);\n return dp[idx][count] = (0LL + choise1)%mod;\n }\n else if(count > 2) return dp[idx][count] = 0LL % mod;\n }\n return dp[idx][count] = 0LL;\n }\n};", "memory": "180675" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int MOD=1e9+7;\n int helper(int index, int seats, string &corridor, vector<vector<int>> &dp)\n {\n if(index==corridor.length())\n {\n if(seats==2)\n return 1;\n return 0;\n }\n if(dp[index][seats]!=-1) return dp[index][seats];\n\n int res=0;\n if(seats<2)\n {\n if(corridor[index]=='S')\n {\n res=helper(index+1, seats+1, corridor,dp);\n }\n else\n {\n res=helper(index+1, seats,corridor,dp);\n }\n }\n else\n {\n if(corridor[index]=='P')\n {\n res=helper(index+1, seats, corridor,dp);\n res+=helper(index+1, 0, corridor, dp);\n }\n else\n {\n res=helper(index+1,1,corridor,dp);\n }\n }\n\n return dp[index][seats]=(res)%MOD;\n }\n int numberOfWays(string corridor) {\n int seats=0;\n vector<vector<int>> dp(corridor.length(), vector<int>(3,-1));\n for(auto it:corridor)\n {\n if(it=='S')\n {\n seats++;\n }\n }\n if(seats%2) return 0;\n\n int res=0;\n return helper(0,0,corridor,dp);\n }\n};", "memory": "185425" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\nint solve(int i,int ct, string &corridor,vector<vector<int>>&dp){\n if(i>=corridor.size()) return ct==2;\n if(dp[i][ct]!=-1) return dp[i][ct];\n int ans=0;\n if(ct==2){\n if(corridor[i]=='P'){\n ans=(solve(i+1,ct,corridor,dp)+solve(i+1,0,corridor,dp));\n } else{\n ans=solve(i+1,1,corridor,dp);\n }\n }else{\n if(corridor[i]=='P'){\n ans=solve(i+1,ct,corridor,dp);\n } else{\n ans=solve(i+1,ct+1,corridor,dp);\n }\n }\n return dp[i][ct]= ans%1000000007;\n}\n int numberOfWays(string corridor) {\n vector<vector<int>>dp(corridor.size(),vector<int>(3,-1));\n return solve(0,0 ,corridor,dp);\n }\n};\n\n/**\n * \n * k = current number of seats\n * i = current position\n * Time complexity = O(N)\n * \n**/\n// class Solution {\n// public:\n// const int MOD = (int)1e9 + 7;\n// long long dp[100001][3];\n// long long getAns(string & a, int i, int k) {\n// if(i >= a.size()) {\n// return k == 2;\n// }\n \n// if(dp[i][k] != -1)return dp[i][k];\n \n// \t\t//if total number of seats in current segment is 2\n// if(k == 2) {\n// if(a[i] == 'P') {\n// \t\t\t\t//choice either to add divider before current plant or not.\n// return dp[i][k] = (getAns(a, i + 1, 0) % MOD + getAns(a, i + 1, k) % MOD) % MOD;\n// }\n// else {\n// \t\t\t\t//necessary to put a divider before current seat, as current count of seat is 2\n// return dp[i][k] = getAns(a, i + 1, 1) % MOD;\n// }\n// }\n// else {\n// \t\t\t//current seat is less than 2 so, move ahead\n// return dp[i][k] = getAns(a, i + 1, k + (a[i] == 'S')) % MOD;\n// }\n \n \n// }\n// int numberOfWays(string corridor) {\n// memset(dp, -1, sizeof(dp));\n// return getAns(corridor, 0, 0); \n// }\n// };", "memory": "190175" }
2,251
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>&#39;S&#39;</code> and <code>&#39;P&#39;</code> where each <code>&#39;S&#39;</code> represents a seat and each <code>&#39;P&#39;</code> represents a plant.</p> <p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 &lt;= i &lt;= n - 1</code>), at most one divider can be installed.</p> <p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> <p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> <pre> <strong>Input:</strong> corridor = &quot;SSPPSPS&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;PPSPSP&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> <pre> <strong>Input:</strong> corridor = &quot;S&quot; <strong>Output:</strong> 0 <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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == corridor.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>corridor[i]</code> is either <code>&#39;S&#39;</code> or <code>&#39;P&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic: \n int generate(const string &corridor, int i, int prev)\n {\n if (i == corridor.size())\n {\n if (prev == 2)\n {\n return 1;\n }\n else\n {\n return 0;\n }\n }\n if (hash[i][prev] != INT_MIN) return hash[i][prev];\n int answer = 0;\n if (corridor[i] == 'S')\n {\n if (prev == 2)\n {\n if (hash[i + 1][1] == INT_MIN)\n {\n hash[i + 1][1] = generate(corridor, i + 1, 1)% 1000000007;\n }\n answer = hash[i + 1][1];\n }\n else \n {\n if (hash[i + 1][prev + 1] == INT_MIN)\n {\n hash[i + 1][prev + 1] = generate(corridor, i + 1, prev + 1)% 1000000007;\n }\n answer = hash[i + 1][prev + 1];\n }\n }\n else\n {\n if (hash[i + 1][prev] == INT_MIN)\n {\n hash[i + 1][prev] = generate(corridor, i + 1, prev) % 1000000007;\n }\n answer = hash[i + 1][prev];\n if (prev == 2)\n {\n if (hash[i + 1][0] == INT_MIN)\n {\n hash[i + 1][0] = generate(corridor, i + 1, 0)% 1000000007;\n }\n answer = (answer + hash[i + 1][0]) % 1000000007;\n //answer += generate(corridor, i + 1, 0);\n } \n }\n return answer;\n }\n int numberOfWays(string corridor) {\n hash.resize(corridor.size() + 1, vector<int>(3, INT_MIN));\n return generate(corridor, 0, 0);\n }\nprivate:\n vector<vector<int>> hash;\n};", "memory": "194925" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n std::stable_partition(nums.begin(), nums.end(), [](auto num) {\n return num >= 0;\n });\n const int middle = nums.size() / 2;\n for (int idx = 1; idx < nums.size(); ++idx) {\n const bool shouldBePositive = idx % 2 == 0;\n const bool positive = nums[idx] >= 0;\n if (shouldBePositive == positive) {\n continue;\n }\n int idxSwap = idx;\n int valSaved = nums[idxSwap];\n do {\n idxSwap = idxSwap >= middle ? 2 * (idxSwap - middle) + 1 : 2 * idxSwap;\n swap(valSaved, nums[idxSwap]);\n } while (idxSwap != idx);\n }\n return std::move(nums);\n }\n};", "memory": "124617" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n std::stable_partition(nums.begin(), nums.end(), [](auto num) {\n return num >= 0;\n });\n const int middle = nums.size() / 2;\n for (int idx = 1; idx < nums.size(); ++idx) {\n const bool shouldBePositive = idx % 2 == 0;\n const bool positive = nums[idx] >= 0;\n if (shouldBePositive == positive) {\n continue;\n }\n int idxSwap = idx;\n int valSaved = nums[idxSwap];\n do {\n idxSwap = idxSwap >= middle ? 2 * (idxSwap - middle) + 1 : 2 * idxSwap;\n swap(valSaved, nums[idxSwap]);\n } while (idxSwap != idx);\n }\n return std::move(nums);\n }\n};", "memory": "124617" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n stable_partition(nums.begin(), nums.end(), [](auto num) {\n return num >= 0;\n });\n const int middle = nums.size() / 2;\n for (int idx = 1; idx < nums.size(); ++idx) {\n const bool shouldBePositive = idx % 2 == 0;\n const bool positive = nums[idx] >= 0;\n if (shouldBePositive == positive) {\n continue;\n }\n int idxSwap = idx;\n int valSaved = nums[idxSwap];\n do {\n idxSwap = idxSwap >= middle ? 2 * (idxSwap - middle) + 1 : 2 * idxSwap;\n swap(valSaved, nums[idxSwap]);\n } while (idxSwap != idx);\n }\n return move(nums);\n }\n};", "memory": "124852" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n int pos=0,neg=1;\n vector<int>ans(nums.size(),0); \n for(int i=0;i<nums.size();i++){\n if(nums[i]>0){\n ans[pos]=nums[i];\n pos+=2;\n }\n else{\n ans[neg]=nums[i];\n neg+=2;\n }\n }\n return ans;\n }\n};", "memory": "124852" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>result(nums.size(),0);\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int posindex=0;\n int negindex=1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0){\n result[negindex]=nums[i];\n negindex+=2;\n }\n else{\n result[posindex]=nums[i];\n posindex+=2;\n }\n }\n return result;\n }\n};\n", "memory": "125087" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>result(nums.size(),0);\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int posindex=0;\n int negindex=1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0){\n result[negindex]=nums[i];\n negindex+=2;\n }\n else{\n result[posindex]=nums[i];\n posindex+=2;\n }\n }\n return result;\n }\n};\n", "memory": "125087" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n int pos=0;\n int neg=1;\n vector<int> result(nums.size());\n for(int i=0;i<nums.size();i++){\n if(nums[i]>0){\n result[pos]=nums[i];\n pos+=2;\n }\n if(nums[i]<0){\n result[neg]=nums[i];\n neg+=2;\n }\n }\n return result;\n }\n};", "memory": "125322" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n int posIdx = 0, negIdx = 1;\n vector<int> ans(nums.size());\n \n for (int num: nums) {\n if (num < 0) {\n ans[negIdx] = num;\n negIdx += 2;\n }\n else {\n ans[posIdx] = num;\n posIdx += 2;\n }\n }\n return ans;\n }\n};", "memory": "125322" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n vector<int> ans(nums.size());\n int posindex=0;\n int negindex=1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]>0){\n ans[posindex]=nums[i];\n posindex+=2;\n }\n else{\n ans[negindex]=nums[i];\n negindex+=2;\n }\n \n }\n\n return ans;\n \n }\n};", "memory": "125557" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n vector<int> ans(nums.size());\n int posIdx = 0;\n int negIdx = 1;\n\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] > 0) {\n ans[posIdx] = nums[i];\n posIdx += 2;\n } else {\n ans[negIdx] = nums[i];\n negIdx += 2;\n }\n }\n\n return ans;\n }\n};", "memory": "125557" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n vector <int> ans(n, 0);\n int pos = 0;\n int neg = 1;\n for(int i = 0; i < n; i++){\n if(nums[i] < 0){\n ans[neg] = nums[i];\n neg += 2;\n }\n else{\n ans[pos] = nums[i];\n pos += 2;\n }\n }\n return ans;\n }\n};", "memory": "125792" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int pos=0,neg=1;\n vector<int> a=nums;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n a[neg]=nums[i];\n neg+=2;\n }\n else\n {\n a[pos]=nums[i];\n pos+=2;\n }\n } \n return a; \n \n }\n};", "memory": "126027" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n int n = nums.size();\n vector <int>arr(n,0);\n int even =0;\n int odd =1;\n for(int i =0 ;i<n;i++){\n if(nums[i] <0 ){\n arr[odd] =nums[i];\n odd+=2;\n }else{\n arr[even]=nums[i];\n even+=2;\n }\n }\n return arr;\n }\n};", "memory": "126027" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n // it pos neg\n int pos=0,neg=1;\n vector<int> ans(nums.size(),0);\nfor(int i=0;i<nums.size();i++){\n if(nums[i]<0){\n ans[neg]=nums[i];\n neg+=2;\n }\n else{\n ans[pos]=nums[i];\n pos+=2;\n }\n}\nreturn ans;\n\n\n }\n};", "memory": "126262" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>vec(nums.size(),0);\n int i=0;\n int j=1;\n for(int n: nums){\n\n if(n>0){\n vec[i]=n;\n i+=2;\n }\n else{\n vec[j]=n;\n j+=2;\n }\n }\n \n \n return vec;\n \n } \n};", "memory": "126262" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int pi = 0;\n int ni = 1;\n vector<int> res(nums.size(), 0);\n\n for (int n : nums) {\n if (n > 0) {\n res[pi] = n;\n pi += 2;\n } else {\n res[ni] = n;\n ni += 2;\n }\n }\n\n return res; \n }\n};", "memory": "126497" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
0
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int pcnt=-1,ncnt=-1;\n vector<int> ans(nums.size());\n for(int i=0;i<nums.size();i++){\n if(nums[i]>0){\n pcnt++;\n ans[2*pcnt] = nums[i];\n }\n else{\n ncnt++;\n ans[2*ncnt + 1] = nums[i];\n }\n }\n return ans;\n }\n};", "memory": "126497" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> ans(n,0);\n int pos=0,neg=1;\n for(int i=0;i<n;i++){\n if(nums[i]<0){\n ans[neg]=nums[i];\n neg+=2;\n }else{\n ans[pos]=nums[i];\n pos+=2;\n }\n }\n return ans;\n }\n};", "memory": "126732" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int temp[nums.size()];\n int a=0,b=1;\n for(int i =0;i<nums.size();i++){\n if(nums[i]>=0){\n temp[a]=nums[i];\n a = a+2;\n }\n else{\n temp[b]=nums[i];\n b=b+2;\n }\n }\n for(int i = 0;i<nums.size();i++){\n nums[i] = temp[i];\n }\n return nums;\n }\n};", "memory": "126732" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n\n\n\n\nint n = nums.size();\n\n\n int ans[n];\n int m = 0;\n int l = 1;\n for (int i = 0; i < n; i++)\n {\n \n if (nums[i] >= 0 && m < n)\n {\n\n ans[m] = nums[i];\n\n m = m + 2;\n }\n\n if (nums[i] < 0 && l < n)\n {\n ans[l] = nums[i];\n \n l = l + 2;\n }\n \n \n }\n\n\nfor (int i = 0; i < n; i++)\n { nums[i]=ans[i];} \n\n\n \n return nums ;\n }\n};", "memory": "126967" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n int arr[n];\n // int n = nums.size();\n int left=0;\n int right =1;\n for( int i=0;i<n;i++){\n if( nums[i]>=0){\n arr[left] = nums[i];\n left+=2;\n }\n else{\n arr[right] = nums[i];\n right+=2;\n }\n }\n for( int i=0;i<n;i++)\n nums[i] = arr[i];\n return nums;\n }\n};", "memory": "127202" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) \n{\n // Dekho bhaiya, basic idea yeh hai ki array mein equal number of positive aur negative elements hai,\n // toh hum do alag arrays banaenge, ek mein positive aur doosre mein negative elements rakh lenge.\n // Jaise order main elements pehle array mein hai, waise hi rakhenge.\n // Phir final result ke liye hum even index pe positive element daalenge aur odd index pe negative element.\n \n int n = nums.size();\n \n // s1 positive elements ke liye aur s2 negative elements ke liye\n int s1 = n / 2, s2 = n - s1;\n \n // Do alag arrays banate hain, ek positive ke liye aur doosra negative ke liye\n int arr1[s1]; // Positive elements ke liye\n int arr2[s2]; // Negative elements ke liye\n \n // Do pointers banate hain, p positive ke liye aur k negative ke liye\n int p = 0, k = 0;\n\n // Pehle loop mein positive aur negative elements ko alag-alag arrays mein daalenge\n for (int i = 0; i < nums.size(); i++)\n {\n if (nums[i] > 0) \n {\n arr1[p++] = nums[i]; // Positive elements ko arr1 mein daal do\n }\n else\n {\n arr2[k++] = nums[i]; // Negative elements ko arr2 mein daal do\n }\n }\n\n // Ab jo initial vector diya gaya tha, usko clear kar do\n nums.clear();\n\n // Ab final nums array ko bharte hain, alternating positions par positive aur negative rakhte hue\n // Dono arrays ke iterators ko 0 par laate hain\n p = 0;\n k = 0;\n \n // Ab final vector ko alternating positive aur negative elements se bharte hain\n for (int i = 0; i < (s1 + s2); i++)\n {\n if (i % 2 == 0) // Even index pe positive element daalo\n {\n nums.push_back(arr1[p++]);\n }\n else // Odd index pe negative element daalo\n {\n nums.push_back(arr2[k++]);\n }\n }\n\n // Final rearranged array return kar do\n return nums;\n}\n\n};", "memory": "127202" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int arranged[nums.size()];\n int pos = 0, neg = 1;\n for (size_t i = 0; i < nums.size(); i++) {\n if (nums[i] > 0) {\n arranged[pos] = nums[i];\n pos += 2;\n } else {\n arranged[neg] = nums[i];\n neg += 2;\n }\n }\n for (size_t i = 0; i < nums.size(); i++) {\n nums[i] = arranged[i];\n }\n return nums;\n }\n};", "memory": "127437" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int nc = 0, k = 0,z=0;\n int n = nums.size();\n for (int i = 0; i < n; i++) {\n if (nums[i] < 0)\n nc++;\n }\n int pc = n-nc;\n int pos[pc], neg[nc];\n for (int i = 0; i < n; i++) {\n if (nums[i] < 0)\n neg[z++] = nums[i];\n else\n pos[k++] = nums[i];\n }\n vector<int>mer(n);\n int r=0,s=0,t=0;\n while(r< pc && s< nc){\n mer[t++] = pos[r++];\n mer[t++] = neg[s++];\n }\n while(r<pc){\n mer[t++] = pos[r++];\n }\n while(s< nc){\n mer[t++] = neg[s++];\n }\n for(int i=0;i<n;i++){\n cout<< mer[i];\n }\n return mer;\n }\n \n};", "memory": "127437" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "int speedup = []{\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n return 0;\n}();\n\n\nclass Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> result (nums.size());\n int i = 0 , j = 1;\n \n for(int k = 0 ; k<nums.size(); k++){\n if(nums[k] < 0 ){\n result[j] = nums[k];\n j = j+2;\n }else {\n result[i] = nums[k];\n i = i+2;\n } \n }\n\n return result;\n \n }\n};", "memory": "127672" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "static const auto x = []() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int posIdx = 0, negIdx = 1;\n vector<int> ans(nums.size());\n \n for (int num: nums) {\n if (num < 0) {\n ans[negIdx] = num;\n negIdx += 2;\n }\n else {\n ans[posIdx] = num;\n posIdx += 2;\n }\n }\n return ans;\n }\n};", "memory": "127672" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> res(n);\n int pos = 0, neg = 1;\n for(const int& it : nums){\n if(it >= 0){\n res[pos] = it;\n pos+=2;\n }else{\n res[neg] = it;\n neg += 2;\n }\n }\n return res;\n\n }\n};\n\nstatic const auto it = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();", "memory": "127907" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> ans(nums.size());\n int pos = 0;\n int neg = 1;\n\n for(int i=0; i<nums.size(); i++)\n {\n if(nums[i] > 0)\n {\n ans[pos] = nums[i];\n pos += 2;\n }\n else\n {\n ans[neg] = nums[i];\n neg += 2;\n }\n }\n return ans;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "127907" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n // if(nums[0]<0){\n // long long j = 1;\n // while(j<nums.size()){\n // if(nums[j]>=0 && nums[0]<0){\n // long long item = nums[j];\n // for(long long k=j; k>0; k--){\n // nums[k] = nums[k-1];\n // }\n // nums[0] = item;\n // break;\n // }\n // j++;\n // }\n // }\n // long long i = 1;\n // while(i<nums.size()){\n // if((nums[i]>=0 && nums[i-1]<0) || (nums[i]<0 && nums[i-1]>=0)){\n // i++;\n // } else {\n // long long j = i+1;\n // while(j<nums.size()){\n // if((nums[j]>=0 && nums[i]<0) || (nums[j]<0 && nums[i]>=0)){\n // long long item = nums[j];\n // for(long long k=j; k>i; k--){\n // nums[k] = nums[k-1];\n // }\n // nums[i] = item;\n // break;\n // }\n // j++;\n // }\n // i++;\n // } \n // }\n // return nums;\n\n //brute force approach\n\n long long n = nums.size();\n long long i=0, j=n/2, k=0, l=n/2;\n long long arr[nums.size()];\n for(long long y=0; y<n; y++){\n if(nums[y]>=0){\n arr[i] = nums[y];\n i++;\n } else {\n arr[j] = nums[y];\n j++;\n }\n }\n nums[0] = arr[k];\n k++;\n nums[1] = arr[l];\n l++;\n for(long long x=2; x<n; x++){\n if(x%2==0){\n nums[x] = arr[k];\n k++;\n }\n else{\n nums[x] = arr[l];\n l++;\n }\n }\n return nums;\n }\n};", "memory": "128142" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int i=0;\n int j=0;\n int n=nums.size();\n long int neww[200000]={0};\n while(i<n/2 && j<n){\n if(nums[j]>0){\n neww[i]=nums[j];\n i++;\n j++; \n }\n else j++;\n }\n j=0;\n while((n/2)<=i<n && j<n){\n if(nums[j]<0){\n neww[i]=nums[j];\n i++;\n j++; \n }\n else j++;\n }\n i=0;\n j=0;\n while(i<n){\n nums[i]=neww[j];\n i+=2;\n j++;\n }\n i=1;\n j=n/2;\n while(i<n){\n nums[i]=neww[j];\n i+=2;\n j++;\n }\n return nums;\n }\n};", "memory": "128142" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n int pos[n/2], neg[n/2];\n\n int p = 0, q = 0;\n\n for(int i=0; i<n; i++) {\n if(nums[i] >= 0) pos[p++] = nums[i];\n else neg[q++] = nums[i];\n }\n\n p = 0, q = 0;\n for(int i=0; i<n; i++) {\n if(i % 2 == 0) nums[i] = pos[p++];\n else nums[i] = neg[q++];\n }\n\n return nums;\n }\n};", "memory": "128377" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n auto neg = stable_partition(nums.begin(), nums.end(), [](int n){ return n > 0; });\n auto pos = nums.begin();\n vector<int> res(nums.size());\n size_t i = 0;\n while (neg != nums.end()) {\n res[i++] = *pos++;\n res[i++] = *neg++;\n }\n return res;\n }\n};", "memory": "128612" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n auto neg = stable_partition(nums.begin(), nums.end(), [](int n){ return n > 0; });\n auto pos = nums.begin();\n vector<int> res(nums.size());\n for (size_t i = 0; neg != nums.end(); ) {\n res[i++] = *pos++;\n res[i++] = *neg++;\n }\n return res;\n }\n};", "memory": "128612" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int pt1 = 0;\n queue<int> posQ, negQ;\n while(pt1 < nums.size()) {\n if(pt1%2 == 0) {\n //postitive number\n if(nums[pt1] == 0) {\n if(!posQ.empty()) {\n nums[pt1] = posQ.front();\n posQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] <= 0) {\n pt2++;\n }\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n } else if(nums[pt1] > 0 && !posQ.empty()) {\n posQ.push(nums[pt1]);\n nums[pt1] = posQ.front();\n posQ.pop();\n } else if(nums[pt1] < 0) {\n if(!posQ.empty()) {\n negQ.push(nums[pt1]);\n nums[pt1] = posQ.front();\n posQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] <= 0) {\n pt2++;\n }\n negQ.push(nums[pt1]);\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n }\n } else {\n // negative number\n if(nums[pt1] == 0) {\n if(!negQ.empty()) {\n nums[pt1] = negQ.front();\n negQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] >= 0) {\n pt2++;\n }\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n } else if(nums[pt1] < 0 && !negQ.empty()) {\n negQ.push(nums[pt1]);\n nums[pt1] = negQ.front();\n negQ.pop();\n } else if(nums[pt1] > 0) {\n if(!negQ.empty()) {\n posQ.push(nums[pt1]);\n nums[pt1] = negQ.front();\n negQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] >= 0) {\n pt2++;\n }\n posQ.push(nums[pt1]);\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n }\n }\n pt1++;\n }\n return nums;\n }\n};", "memory": "128847" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n\n\n queue<int>q;\n int n=nums.size();\n\n for(int i=0;i<n;i++){\n\n if(nums[i]<=0) {\n q.push(nums[i]);\n }\n\n }\n \n vector<int>ans(n);\n int k=0;\n\n for(int i=0;i<n;i++){\n\n if(nums[i]>0) {\n ans[k++]=nums[i];\n ans[k++]=q.front();\n q.pop();\n }\n\n }\n\n return ans;\n\n\n\n\n\n }\n};", "memory": "129082" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int pt1 = 0;\n queue<int> posQ, negQ;\n while(pt1 < nums.size()) {\n if(pt1%2 == 0) {\n //postitive number\n if(nums[pt1] == 0) {\n if(!posQ.empty()) {\n nums[pt1] = posQ.front();\n posQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] <= 0) {\n pt2++;\n }\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n } else if(nums[pt1] > 0 && !posQ.empty()) {\n posQ.push(nums[pt1]);\n nums[pt1] = posQ.front();\n posQ.pop();\n } else if(nums[pt1] < 0) {\n if(!posQ.empty()) {\n negQ.push(nums[pt1]);\n nums[pt1] = posQ.front();\n posQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] <= 0) {\n pt2++;\n }\n negQ.push(nums[pt1]);\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n }\n } else {\n // negative number\n if(nums[pt1] == 0) {\n if(!negQ.empty()) {\n nums[pt1] = negQ.front();\n negQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] >= 0) {\n pt2++;\n }\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n } else if(nums[pt1] < 0 && !negQ.empty()) {\n negQ.push(nums[pt1]);\n nums[pt1] = negQ.front();\n negQ.pop();\n } else if(nums[pt1] > 0) {\n if(!negQ.empty()) {\n posQ.push(nums[pt1]);\n nums[pt1] = negQ.front();\n negQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] >= 0) {\n pt2++;\n }\n posQ.push(nums[pt1]);\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n }\n }\n pt1++;\n }\n return nums;\n }\n};", "memory": "129317" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int pt1 = 0;\n queue<int> posQ, negQ;\n while(pt1 < nums.size()) {\n if(pt1%2 == 0) {\n //postitive number\n if(nums[pt1] == 0) {\n if(!posQ.empty()) {\n nums[pt1] = posQ.front();\n posQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] <= 0) {\n pt2++;\n }\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n } else if(nums[pt1] > 0 && !posQ.empty()) {\n posQ.push(nums[pt1]);\n nums[pt1] = posQ.front();\n posQ.pop();\n } else if(nums[pt1] < 0) {\n if(!posQ.empty()) {\n negQ.push(nums[pt1]);\n nums[pt1] = posQ.front();\n posQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] <= 0) {\n pt2++;\n }\n negQ.push(nums[pt1]);\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n }\n } else {\n // negative number\n if(nums[pt1] == 0) {\n if(!negQ.empty()) {\n nums[pt1] = negQ.front();\n negQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] >= 0) {\n pt2++;\n }\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n } else if(nums[pt1] < 0 && !negQ.empty()) {\n negQ.push(nums[pt1]);\n nums[pt1] = negQ.front();\n negQ.pop();\n } else if(nums[pt1] > 0) {\n if(!negQ.empty()) {\n posQ.push(nums[pt1]);\n nums[pt1] = negQ.front();\n negQ.pop();\n } else {\n int pt2 = pt1 + 1;\n while(nums[pt2] >= 0) {\n pt2++;\n }\n posQ.push(nums[pt1]);\n nums[pt1] = nums[pt2];\n nums[pt2] = 0;\n }\n }\n }\n pt1++;\n }\n return nums;\n }\n};", "memory": "129317" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "#include <vector>\nclass Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n int n = nums.size();\n vector<int> positive(n/2);\n vector<int> negative(n/2);\n int i = 0, j = 0;\n for(int x = 0; x < n; x++){\n if(nums[x] < 0){\n negative[i] = nums[x];\n i++;\n }\n else{\n positive[j] = nums[x];\n j++;\n }\n }\n i = 0, j = 0;\n for(int x = 0; x < n; x++){\n if(x%2 == 0){\n nums[x] = positive[i];\n i++;\n }\n else{\n nums[x] = negative[j];\n j++;\n }\n }\n return nums;\n }\n};", "memory": "129552" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n\n int *arr=new int[n];\n int i=0;\n int j=1;\n int index=0;\n while(index<n){\n if(nums[index]>=0){\n arr[i]=nums[index];\n index++;\n i=i+2;\n }\n else{\n arr[j]=nums[index];\n index++;\n j=j+2;\n }\n \n }\n nums.clear();\n for(int i=0;i<n;i++){\n nums.push_back(arr[i]);\n \n }\n return nums;\n }\n};", "memory": "129787" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n cin.tie(nullptr) -> sync_with_stdio(false);\n int n = nums.size();\n // vector for positive element\n vector<int> v1(n/2);\n int count1 = 0;\n for(int i=0; i<n; i++){\n if(nums[i]>0){\n v1[count1] = nums[i];\n count1++;\n }\n }\n\n // vector for negative element\n vector<int> v2(n/2);\n int count2 = 0;\n for(int i=0; i<n; i++){\n if(nums[i]<0){\n v2[count2] = nums[i];\n count2++;\n }\n }\n\n // now filling the main vector again\n int p = 0;\n int q = 0;\n for(int i=0; i<n; i++){\n if(i%2==0) nums[i] = v1[p++];\n else nums[i] = v2[q++];\n }\n return nums;\n }\n};", "memory": "130022" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n cin.tie(nullptr) -> sync_with_stdio(false);\n int n = nums.size();\n \n vector<int> v1(n/2); // vector for positive element\n vector<int> v2(n/2); // vector for negative element\n int count1 = 0;\n int count2 = 0;\n for(int i=0; i<n; i++){\n if(nums[i]>0) v1[count1++] = nums[i];\n else v2[count2++] = nums[i];\n }\n\n // now filling the main vector again\n int p = 0;\n int q = 0;\n for(int i=0; i<n; i++){\n if(i%2==0) nums[i] = v1[p++];\n else nums[i] = v2[q++];\n }\n return nums;\n }\n};", "memory": "130022" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>ans=nums;\n int k=0,j=0;\n for(int i=0;i<ans.size();i++)\n {\n if(ans[i]>=0)\n {\n nums[2*j]=ans[i];\n j++;\n }\n else\n {\n nums[2*k+1]=ans[i];\n k++;\n }\n }\n return nums;\n }\n};", "memory": "130257" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int s=nums.size();\n vector<int> a(s); \n int p=0;\n int n=1; \n for (int i = 0; i < nums.size(); i ++){\n if (nums[i]>0){\n a[p]=nums[i];\n p+=2; \n }\n if (nums[i]<0){\n a[n]=nums[i];\n n+=2; \n }\n }\n nums = a;\n return nums;\n\n \n }\n};", "memory": "130257" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> temp(n);\n int k=0,j=1;\n for(int i=0;i<n;i++){\n if(nums[i]<0){\n temp[j]=nums[i];\n j=j+2;\n }\n else{\n temp[k]=nums[i];\n k=k+2;\n }\n }\n for(int i=0;i<n;i++){\n nums[i]=temp[i];\n }\n return nums;\n }\n};", "memory": "130492" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> negatives;\n\n int low = -1;\n for(int i = 0; i < nums.size(); i++) {\n if(nums[i] > 0) {\n swap(nums[i], nums[++low]);\n } else {\n negatives.push_back(nums[i]);\n }\n }\n\n int high = nums.size() - 2;\n for(int i = (nums.size() / 2) - 1; i >= 0; i--) {\n if(nums[i] > 0) {\n swap(nums[i], nums[high]);\n high -= 2;\n }\n }\n\n int index = 1;\n for(int i = 0; i < negatives.size(); i++) {\n nums[index] = negatives[i];\n index += 2;\n }\n\n return nums;\n }\n};", "memory": "131667" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n int start=0,end=0;\n vector<int> vec;\n while(end<nums.size()&&start<nums.size()){\n while(start<nums.size()&&nums[start]<0) start++;\n if(nums[start]>0){\n vec.push_back(nums[start]);\n start++;}\n while(end<nums.size()&&nums[end]>0) end++;\n if(nums[end]<0){\n vec.push_back(nums[end]);\n end++;}\n }\n return vec;\n }\n};", "memory": "131902" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n queue<int> q1;\n queue<int> q2;\n for(int i = 0; i< nums.size();i++){\n if(nums[i] >= 0){\n q1.push(nums[i]);\n }\n else{ \n q2.push(nums[i]);\n }\n }\n for(int i = 0; i< nums.size();i++){\n if(i%2== 0){\n nums[i] = q1.front();\n q1.pop();\n }\n else{\n nums[i] = q2.front();\n q2.pop();\n }\n }\n return nums;\n }\n};", "memory": "132137" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n queue<int> qp,qn;\n for(int i=0;i<nums.size();i++){\n if(nums[i]>0){\n qp.push(nums[i]);\n }\n else{\n qn.push(nums[i]);\n }\n }\n int flag=0;\n for(int i=0;i<nums.size();i++){\n if(flag==0){\n nums[i]=qp.front();\n flag=1;\n qp.pop();\n }\n else{\n nums[i]=qn.front();\n flag=0;\n qn.pop();\n }\n }\n\n return nums;\n }\n};", "memory": "132137" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int N=nums.size();\n vector<int> ans;\n int p=0;\n int n=0;\n \n while(p<N && n<N){\n while(p<N && nums[p]<0) p++;\n while(n<N && nums[n]>0) n++;\n if(p<N && n<N){\n ans.push_back(nums[p]);\n ans.push_back(nums[n]);\n }\n p++;\n n++;\n }\n return ans;\n\n\n \n }\n};", "memory": "132372" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n/* if(nums[i]<0 && nums[j]>=0){\n swap(nums[i], nums[j]);\n i+=2;\n j+=2;\n }\n else if(nums[i] >= 0 && i < nums.size()) i+=2;\n else if(nums[j] < 0 && j < nums.size()) j+=2;\n*/\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> ans;\n\n int i = 0, j = 0;\n while(i<nums.size() || j<nums.size())\n {\n while(i < nums.size() && nums[i] <= 0)\n i++;\n if(i < nums.size())\n ans.push_back(nums[i++]);\n while(j < nums.size() && nums[j] > 0)\n j++;\n if(j < nums.size())\n ans.push_back(nums[j++]);\n }\n return ans;\n }\n};", "memory": "132372" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> ans;\n int i=0,j=0;\n int n=nums.size();\n while(i<n && j<n){\n while(nums[i]<0){\n i++;\n }\n while(nums[j]>0){\n j++;\n }\n ans.push_back(nums[i]);\n i++;\n ans.push_back(nums[j]);\n j++;\n } \n return ans; \n }\n};", "memory": "132607" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> v;\n int pv=0;\n int nv=0;\n\n while(pv<=n-1 && nv<=n-1){\n if(nums[pv]>0){\n v.emplace_back(nums[pv++]);\n }\n else{\n while(nums[pv]<0){\n pv++;\n }\n v.emplace_back(nums[pv++]);\n }\n if(nums[nv]<0){\n v.emplace_back(nums[nv++]);\n }\n else{\n while(nums[nv]>0){\n nv++;\n }\n v.emplace_back(nums[nv++]);\n }\n }\n return v;\n }\n};", "memory": "132607" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int f1 = 0,f2 = 0;\n int i = 0,j = 0;\n int n = nums.size();\n vector <int> ans;\n while(i < n) {\n while(!f1 && i < n) {\n if(nums[i] >= 0) {\n f1 = 1;\n break;\n }\n i++;\n }\n while(!f2 && j < n) {\n if(nums[j] < 0) {\n f2 = 1;\n break;\n }\n j++;\n }\n if(f1 && f2) {\n\n f1 = 0;\n f2 = 0;\n ans.push_back(nums[i]);\n ans.push_back(nums[j]);\n i++;\n j++;\n }\n }\n return ans;\n }\n};", "memory": "132842" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> neg;\n int index=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0){\n neg.push_back(nums[i]);\n }else{\n nums[index++]=nums[i];\n }\n }\n nums.resize(index);\n int j=1;\n for(int i=0;i<neg.size();i++){\n if(j<nums.size()){\n nums.insert(nums.begin()+j,neg[i]);\n }else{\n nums.push_back(neg[i]);\n }\n j+=2;\n }\n return nums;\n }\n};", "memory": "132842" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n // Count the number of positive and negative numbers\n int posCount = 0, negCount = 0;\n for (int i : nums) {\n if (i > 0) posCount++;\n else negCount++;\n }\n\n // Allocate temp vector with the correct size\n std::vector<std::vector<int>> temp(2, std::vector<int>(std::max(posCount, negCount)));\n\n int posIndex = 0, negIndex = 0;\n for (int i : nums) {\n if (i > 0) {\n temp[0][posIndex++] = i;\n } else {\n temp[1][negIndex++] = i;\n }\n }\n\n // Prepare the result vector\n std::vector<int> result;\n result.reserve(nums.size());\n\n // Interleave positives and negatives\n posIndex = 0;\n negIndex = 0;\n for (int i = 0; i < nums.size(); ++i) {\n if (i % 2 == 0) {\n result.push_back(temp[0][posIndex++]);\n } else {\n result.push_back(temp[1][negIndex++]);\n }\n }\n\n return result;\n }\n};", "memory": "133077" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n // Count the number of positive and negative numbers\n int posCount = 0, negCount = 0;\n for (int i : nums) {\n if (i > 0) posCount++;\n else negCount++;\n }\n\n // Allocate temp vector with the correct size\n std::vector<std::vector<int>> temp(2, std::vector<int>(std::max(posCount, negCount)));\n\n int posIndex = 0, negIndex = 0;\n for (int i : nums) {\n if (i > 0) {\n temp[0][posIndex++] = i;\n } else {\n temp[1][negIndex++] = i;\n }\n }\n\n // Prepare the result vector\n std::vector<int> result;\n result.reserve(nums.size());\n\n // Interleave positives and negatives\n posIndex = 0;\n negIndex = 0;\n for (int i = 0; i < nums.size(); ++i) {\n if (i % 2 == 0) {\n result.push_back(temp[0][posIndex++]);\n } else {\n result.push_back(temp[1][negIndex++]);\n }\n }\n\n return result;\n }\n};", "memory": "133312" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int a[nums.size()];\n int k=0,j=1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]>0) {a[k]=nums[i];k+=2;}\n else {a[j]=nums[i];j+=2;}\n }\n vector<int> v;\n for(int i=0;i<nums.size();i++){\n v.push_back(a[i]);\n }\n return v;\n }\n};", "memory": "133312" }