Skip to content

Commit 023153a

Browse files
committed
update: 添加问题“2147.分隔长廊的方案数”的代码和题解 (#1224)
2147: AC.cpp (#1223) + word(en)早上 没有座位竟然是0种方案数,按理说一个隔板都不放不应该是唯一的一种方案吗。 cpp - AC,86.11%,100.00% 、AC,96.53%,100.00% Signed-off-by: LetMeFly666 <[email protected]>
1 parent 6ffc83f commit 023153a

9 files changed

+243
-61
lines changed

.commitmsg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
3606: AC.cpp - AC,59.14%,92.47% (#1221) + word(en)
1+
2147: AC.cpp (#1223) + word(en)早上
2+
3+
没有座位竟然是0种方案数,按理说一个隔板都不放不应该是唯一的一种方案吗。
4+
cpp - AC,86.11%,100.00% 、AC,96.53%,100.00%
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-14 17:30:12
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-14 17:37:56
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 1e9 + 7;
13+
class Solution {
14+
public:
15+
int numberOfWays(string& corridor) {
16+
ll ans = 1;
17+
int now = 0;
18+
bool ing = false; // 正在处理两块座位之间的绿植
19+
bool atLeast2 = false;
20+
for (char c : corridor) {
21+
if (c == 'S') {
22+
if (ing) {
23+
ans = ans * (now + 1) % MOD;
24+
ing = false;
25+
now = 1;
26+
} else {
27+
now++;
28+
if (now == 2) {
29+
ing = true;
30+
now = 0;
31+
atLeast2 = true;
32+
}
33+
}
34+
} else { // 'P'
35+
if (ing) {
36+
now++;
37+
}
38+
}
39+
}
40+
if (!ing && now != 0 || !atLeast2) {
41+
return 0;
42+
}
43+
return static_cast<int>(ans);
44+
}
45+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-14 18:28:41
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-14 18:29:02
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 1e9 + 7;
13+
class Solution {
14+
public:
15+
int numberOfWays(string& corridor) {
16+
ll ans = 1;
17+
int now = 0;
18+
bool ing = false; // 正在处理两块座位之间的绿植
19+
bool atLeast2 = false;
20+
for (char c : corridor) {
21+
if (c == 'S') {
22+
if (ing) {
23+
ans = ans * (now + 1) % MOD;
24+
ing = false;
25+
now = 1;
26+
} else {
27+
now++;
28+
if (now == 2) {
29+
ing = true;
30+
now = 0;
31+
atLeast2 = true;
32+
}
33+
}
34+
} else { // 'P'
35+
if (ing) {
36+
now++;
37+
}
38+
}
39+
}
40+
if (!ing || !atLeast2) {
41+
return 0;
42+
}
43+
return static_cast<int>(ans);
44+
}
45+
};

Codes/3606-coupon-code-validator.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
22
* @Author: LetMeFly
3-
* @Date: 2025-12-13 22:26:53
3+
* @Date: 2025-12-13 22:41:43
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-12-13 22:42:38
5+
* @LastEditTime: 2025-12-13 22:42:29
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS CANNOT PASS
1211
class Solution {
1312
private:
1413
inline bool is_ok(string& s) {
@@ -29,13 +28,13 @@ class Solution {
2928
if (!is_ok(code[i])) {
3029
continue;
3130
}
32-
if (businessLine[i][0] == 'e') {
31+
if (businessLine[i] == "electronics") {
3332
electronics.push_back(code[i]);
34-
} else if (businessLine[i][0] == 'g') {
33+
} else if (businessLine[i] == "grocery") {
3534
grocery.push_back(code[i]);
36-
} else if (businessLine[i][0] == 'p') {
35+
} else if (businessLine[i] == "pharmacy") {
3736
pharmacy.push_back(code[i]);
38-
} else {
37+
} else if (businessLine[i] == "restaurant") {
3938
restaurant.push_back(code[i]);
4039
}
4140
}

Codes/3606-coupon-code-validator_AC.cpp

Lines changed: 0 additions & 53 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@
712712
|2132.用邮票贴满网格图|困难|<a href="https://leetcode.cn/problems/stamping-the-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/14/LeetCode%202132.%E7%94%A8%E9%82%AE%E7%A5%A8%E8%B4%B4%E6%BB%A1%E7%BD%91%E6%A0%BC%E5%9B%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135002925" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/2566644/letmefly-2132yong-you-piao-tie-man-wang-znigh/" target="_blank">LeetCode题解</a>|
713713
|2138.将字符串拆分为若干长度为k的组|简单|<a href="https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/22/LeetCode%202138.%E5%B0%86%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%86%E5%88%86%E4%B8%BA%E8%8B%A5%E5%B9%B2%E9%95%BF%E5%BA%A6%E4%B8%BAk%E7%9A%84%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148844890" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/solutions/3706437/letmefly-2138jiang-zi-fu-chuan-chai-fen-acsxz/" target="_blank">LeetCode题解</a>|
714714
|2140.解决智力问题|中等|<a href="https://leetcode.cn/problems/solving-questions-with-brainpower/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/03/LeetCode%202140.%E8%A7%A3%E5%86%B3%E6%99%BA%E5%8A%9B%E9%97%AE%E9%A2%98/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146991317" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/solving-questions-with-brainpower/solutions/3639601/letmefly-2140jie-jue-zhi-li-wen-ti-ji-yi-pxc4/" target="_blank">LeetCode题解</a>|
715+
|2147.分隔长廊的方案数|困难|<a href="https://leetcode.cn/problems/number-of-ways-to-divide-a-long-corridor/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/14/LeetCode%202147.%E5%88%86%E9%9A%94%E9%95%BF%E5%BB%8A%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155916226" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-ways-to-divide-a-long-corridor/solutions/3856839/letmefly-2147fen-ge-chang-lang-de-fang-a-tn48/" target="_blank">LeetCode题解</a>|
715716
|2171.拿出最少数目的魔法豆|中等|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/18/LeetCode%202171.%E6%8B%BF%E5%87%BA%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E9%AD%94%E6%B3%95%E8%B1%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135682601" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/solutions/2609831/letmefly-2171na-chu-zui-shao-shu-mu-de-m-jima/" target="_blank">LeetCode题解</a>|
716717
|2176.统计数组中相等且可以被整除的数对|简单|<a href="https://leetcode.cn/problems/count-equal-and-divisible-pairs-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/17/LeetCode%202176.%E7%BB%9F%E8%AE%A1%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9B%B8%E7%AD%89%E4%B8%94%E5%8F%AF%E4%BB%A5%E8%A2%AB%E6%95%B4%E9%99%A4%E7%9A%84%E6%95%B0%E5%AF%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147315213" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-equal-and-divisible-pairs-in-an-array/solutions/3654492/letmefly-2176tong-ji-shu-zu-zhong-xiang-643mr/" target="_blank">LeetCode题解</a>|
717718
|2178.拆分成最多数目的正偶数之和|中等|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/06/LeetCode%202178.%E6%8B%86%E5%88%86%E6%88%90%E6%9C%80%E5%A4%9A%E6%95%B0%E7%9B%AE%E7%9A%84%E6%AD%A3%E5%81%B6%E6%95%B0%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131567568" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/solutions/2332513/letmefly-2178chai-fen-cheng-zui-duo-shu-m44er/" target="_blank">LeetCode题解</a>|
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
title: 2147.分隔长廊的方案数:非Hard组合数学
3+
date: 2025-12-14 18:20:28
4+
tags: [题解, LeetCode, 困难, 数学, 组合数学, 排列组合, 字符串, 动态规划]
5+
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2021/12/04/1.png
7+
---
8+
9+
# 【LetMeFly】2147.分隔长廊的方案数:非Hard组合数学
10+
11+
力扣题目链接:[https://leetcode.cn/problems/number-of-ways-to-divide-a-long-corridor/](https://leetcode.cn/problems/number-of-ways-to-divide-a-long-corridor/)
12+
13+
<p>在一个图书馆的长廊里,有一些座位和装饰植物排成一列。给你一个下标从 <strong>0</strong>&nbsp;开始,长度为 <code>n</code>&nbsp;的字符串&nbsp;<code>corridor</code>&nbsp;,它包含字母&nbsp;<code>'S'</code> 和&nbsp;<code>'P'</code>&nbsp;,其中每个&nbsp;<code>'S'</code>&nbsp;表示一个座位,每个&nbsp;<code>'P'</code>&nbsp;表示一株植物。</p>
14+
15+
<p>在下标 <code>0</code>&nbsp;的左边和下标 <code>n - 1</code>&nbsp;的右边 <strong>已经</strong>&nbsp;分别各放了一个屏风。你还需要额外放置一些屏风。每一个位置&nbsp;<code>i - 1</code> 和&nbsp;<code>i</code>&nbsp;之间(<code>1 &lt;= i &lt;= n - 1</code>),至多能放一个屏风。</p>
16+
17+
<p>请你将走廊用屏风划分为若干段,且每一段内都 <strong>恰好有两个座位</strong>&nbsp;,而每一段内植物的数目没有要求。可能有多种划分方案,如果两个方案中有任何一个屏风的位置不同,那么它们被视为 <strong>不同</strong> 方案。</p>
18+
19+
<p>请你返回划分走廊的方案数。由于答案可能很大,请你返回它对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;的结果。如果没有任何方案,请返回&nbsp;<code>0</code>&nbsp;。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;"></p>
26+
27+
<pre><b>输入:</b>corridor = "SSPPSPS"
28+
<b>输出:</b>3
29+
<b>解释:</b>总共有 3 种不同分隔走廊的方案。
30+
上图中黑色的竖线表示已经放置好的屏风。
31+
上图每种方案中,每一段都恰好有 <strong>两个</strong>&nbsp;座位。
32+
</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;"></p>
37+
38+
<pre><b>输入:</b>corridor = "PPSPSP"
39+
<b>输出:</b>1
40+
<b>解释:</b>只有 1 种分隔走廊的方案,就是不放置任何屏风。
41+
放置任何的屏风都会导致有一段无法恰好有 2 个座位。
42+
</pre>
43+
44+
<p><strong>示例 3:</strong></p>
45+
46+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;"></p>
47+
48+
<pre><b>输入:</b>corridor = "S"
49+
<b>输出:</b>0
50+
<b>解释:</b>没有任何方案,因为总是有一段无法恰好有 2 个座位。
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>n == corridor.length</code></li>
59+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
60+
<li><code>corridor[i]</code>&nbsp;要么是&nbsp;<code>'S'</code>&nbsp;,要么是&nbsp;<code>'P'</code> 。</li>
61+
</ul>
62+
63+
64+
65+
## 解题方法:遍历
66+
67+
从左往右遍历,每出现总计两个座位就要进行一次分隔,本次分隔方案数为两个座位中后一个座位与**下一个**座位之间的绿植数加一。(若后续再无座位,则不需要放置隔板)
68+
69+
总方案数就是每次放置隔板时的方案数之积。
70+
71+
额外注意,本题给定的答案中,若没有座位,则输出0而非“一个隔板都不放的这唯一一种方案”1。
72+
73+
### 具体做法
74+
75+
使用一个变量ing记录当前是否处在两个座位之后下一个座位之前的数绿植状态,使用一个变量now记录当前总计座位数或绿植数,遍历时候使用几个if-else就好了。
76+
77+
额外注意,可以使用一个变量atLeast2记录是否至少有两个座位,遍历过程中一旦出现累计两个座位则将该值赋值为true。
78+
79+
最终若不是在数绿植状态(不是刚好两个座位后)或一共也没有两个座位,返回0。
80+
81+
### 时空复杂度分析
82+
83+
+ 时间复杂度$O(len(corridor))$
84+
+ 空间复杂度$O(1)$
85+
86+
### AC代码
87+
88+
#### C++
89+
90+
```cpp
91+
/*
92+
* @LastEditTime: 2025-12-14 17:37:56
93+
*/
94+
typedef long long ll;
95+
const ll MOD = 1e9 + 7;
96+
class Solution {
97+
public:
98+
int numberOfWays(string& corridor) {
99+
ll ans = 1;
100+
int now = 0;
101+
bool ing = false; // 正在处理两块座位之间的绿植
102+
bool atLeast2 = false;
103+
for (char c : corridor) {
104+
if (c == 'S') {
105+
if (ing) {
106+
ans = ans * (now + 1) % MOD;
107+
ing = false;
108+
now = 1;
109+
} else {
110+
now++;
111+
if (now == 2) {
112+
ing = true;
113+
now = 0;
114+
atLeast2 = true;
115+
}
116+
}
117+
} else { // 'P'
118+
if (ing) {
119+
now++;
120+
}
121+
}
122+
}
123+
if (!ing || !atLeast2) {
124+
return 0;
125+
}
126+
return static_cast<int>(ans);
127+
}
128+
};
129+
```
130+
131+
* 执行用时分布8ms击败96.53%
132+
* 消耗内存分布26.95MB击败100.00%
133+
134+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/155916226)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/14/LeetCode%202147.%E5%88%86%E9%9A%94%E9%95%BF%E5%BB%8A%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0/)哦~
135+
>
136+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/LeetCode 3531.统计被覆盖的建筑.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: 3531.统计被覆盖的建筑:最大最小值
33
date: 2025-12-11 18:39:45
44
tags: [题解, LeetCode, 中等, 数组, 排序]
55
categories: [题解, LeetCode]
6+
index_img: https://pic.leetcode.cn/1745660407-qtNUjI-telegram-cloud-photo-size-5-6212982906394101085-m.jpg
67
---
78

89
# 【LetMeFly】3531.统计被覆盖的建筑:最大最小值

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,11 @@ categories: [自用]
16661666
|dome|n. 穹顶,圆屋顶,穹顶建筑物|
16671667
|||
16681668
|mercantile|adj. 商业化,贸易的|
1669+
|||
1670+
|flannel|n. 法兰绒|
1671+
|seaman|n. 海员,水兵,水手|
1672+
1673+
16691674

16701675
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
16711676
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)