Skip to content

Commit 5e4aa13

Browse files
author
oushihao
committed
feat(remove-colored-pieces-if-both-neighbors-are-the-same-color): 贪心算法
1 parent 6a85b01 commit 5e4aa13

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

assets/data/category.json

+6
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,12 @@
17911791
"title": "最长递增子序列",
17921792
"path": "../../problemset/longest-increasing-subsequence/README.md",
17931793
"difficulty": "中等"
1794+
},
1795+
{
1796+
"id": 2038,
1797+
"title": "如果相邻两个颜色均相同则删除当前颜色",
1798+
"path": "../../problemset/remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md",
1799+
"difficulty": "中等"
17941800
}
17951801
]
17961802
},

assets/data/problems.json

+10
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,16 @@
21892189
"url": "https://leetcode-cn.com/problems/maximum-difference-between-increasing-elements/",
21902190
"path": "../../problemset/maximum-difference-between-increasing-elements/README.md"
21912191
},
2192+
{
2193+
"id": 2038,
2194+
"title": {
2195+
"cn": "如果相邻两个颜色均相同则删除当前颜色",
2196+
"en": "remove-colored-pieces-if-both-neighbors-are-the-same-color"
2197+
},
2198+
"difficulty": "中等",
2199+
"url": "https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/",
2200+
"path": "../../problemset/remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md"
2201+
},
21922202
{
21932203
"id": 2039,
21942204
"title": {

assets/docs/CATEGORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
| 68. [文本左右对齐](../../problemset/text-justification/README.md) | 困难 |
370370
| 122. [买卖股票的最佳时机 II](../../problemset/best-time-to-buy-and-sell-stock-2/README.md) | 中等 |
371371
| 300. [最长递增子序列](../../problemset/longest-increasing-subsequence/README.md) | 中等 |
372+
| 2038. [如果相邻两个颜色均相同则删除当前颜色](../../problemset/remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md) | 中等 |
372373

373374
## 二叉树
374375

assets/docs/PROBLEMS.md

+2
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@
438438

439439
[2016. 增量元素之间的最大差值](../../problemset/maximum-difference-between-increasing-elements/README.md)
440440

441+
[2038. 如果相邻两个颜色均相同则删除当前颜色](../../problemset/remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md)
442+
441443
[2039. 网络空闲的时刻](../../problemset/the-time-when-the-network-becomes-idle/README.md)
442444

443445
[2043. 简易银行系统](../../problemset/simple-bank-system/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 如果相邻两个颜色均相同则删除当前颜色
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/
6+
7+
## 题目
8+
9+
总共有 `n`  个颜色片段排成一列,每个颜色片段要么是  `'A'`  要么是  `'B'` 。给你
10+
一个长度为  `n`  的字符串  `colors` ,其中  `colors[i]`  表示第  `i`  个颜色片段
11+
的颜色。
12+
13+
Alice 和 Bob 在玩一个游戏,他们 **轮流**  从这个字符串中删除颜色。Alice 先手  。
14+
15+
- 如果一个颜色片段为 `'A'`  且 **相邻两个颜色**  都是颜色 `'A'` ,那么 Alice 可
16+
以删除该颜色片段。Alice **不可以**  删除任何颜色  `'B'`  片段。
17+
- 如果一个颜色片段为 `'B'`  且 **相邻两个颜色**  都是颜色 `'B'` ,那么 Bob 可以
18+
删除该颜色片段。Bob **不可以**  删除任何颜色 `'A'`  片段。
19+
- Alice 和 Bob **不能  **从字符串两端删除颜色片段。
20+
- 如果其中一人无法继续操作,则该玩家 ****  掉游戏且另一玩家 **获胜** 。
21+
22+
假设 Alice 和 Bob 都采用最优策略,如果 Alice 获胜,请返回  `true`,否则 Bob 获胜
23+
,返回  `false`
24+
25+
### 示例
26+
27+
#### 示例 1:
28+
29+
```
30+
输入:colors = "AAABABB"
31+
输出:true
32+
解释:
33+
AAABABB -> AABABB
34+
Alice 先操作。
35+
她删除从左数第二个 'A' ,这也是唯一一个相邻颜色片段都是 'A' 的 'A' 。
36+
37+
现在轮到 Bob 操作。
38+
Bob 无法执行任何操作,因为没有相邻位置都是 'B' 的颜色片段 'B' 。
39+
因此,Alice 获胜,返回 true 。
40+
```
41+
42+
#### 示例 2:
43+
44+
```
45+
输入:colors = "AA"
46+
输出:false
47+
解释:
48+
Alice 先操作。
49+
只有 2 个 'A' 且它们都在字符串的两端,所以她无法执行任何操作。
50+
因此,Bob 获胜,返回 false 。
51+
```
52+
53+
#### 示例 3:
54+
55+
```
56+
输入:colors = "ABBBBBBBAAA"
57+
输出:false
58+
解释:
59+
ABBBBBBBAAA -> ABBBBBBBAA
60+
Alice 先操作。
61+
她唯一的选择是删除从右数起第二个 'A' 。
62+
63+
ABBBBBBBAA -> ABBBBBBAA
64+
接下来轮到 Bob 操作。
65+
他有许多选择,他可以选择任何一个 'B' 删除。
66+
67+
然后轮到 Alice 操作,她无法删除任何片段。
68+
所以 Bob 获胜,返回 false 。
69+
```
70+
71+
## 解题
72+
73+
```typescript
74+
/**
75+
* 贪心算法
76+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
77+
* @param colors
78+
*/
79+
export function winnerOfGame(colors: string): boolean {
80+
let aCount = 0;
81+
let bCount = 0;
82+
83+
for (let i = 1; i < colors.length - 1; i++) {
84+
if (colors[i] === 'A' && colors[i - 1] === 'A' && colors[i + 1] === 'A') {
85+
aCount++;
86+
} else if (
87+
colors[i] === 'B' &&
88+
colors[i - 1] === 'B' &&
89+
colors[i + 1] === 'B'
90+
) {
91+
bCount++;
92+
}
93+
}
94+
return aCount > bCount;
95+
}
96+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { winnerOfGame } from '.';
2+
3+
describe('如果相邻两个颜色均相同则删除当前颜色', () => {
4+
testCase(winnerOfGame);
5+
});
6+
7+
function testCase(fn: (colors: string) => boolean) {
8+
it('示例一', () => {
9+
const colors = 'AAABABB';
10+
const expected = true;
11+
expect(fn(colors)).toBe(expected);
12+
});
13+
14+
it('示例二', () => {
15+
const colors = 'AA';
16+
const expected = false;
17+
expect(fn(colors)).toBe(expected);
18+
});
19+
20+
it('示例三', () => {
21+
const colors = 'ABBBBBBBAAA';
22+
const expected = false;
23+
expect(fn(colors)).toBe(expected);
24+
});
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 贪心算法
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
4+
* @param colors
5+
*/
6+
export function winnerOfGame(colors: string): boolean {
7+
let aCount = 0;
8+
let bCount = 0;
9+
10+
for (let i = 1; i < colors.length - 1; i++) {
11+
if (colors[i] === 'A' && colors[i - 1] === 'A' && colors[i + 1] === 'A') {
12+
aCount++;
13+
} else if (
14+
colors[i] === 'B' &&
15+
colors[i - 1] === 'B' &&
16+
colors[i + 1] === 'B'
17+
) {
18+
bCount++;
19+
}
20+
}
21+
return aCount > bCount;
22+
}

0 commit comments

Comments
 (0)