Skip to content

Commit a65a526

Browse files
committed
update: 添加问题“2938.区分黑球与白球”的代码和题解
1 parent 69871d2 commit a65a526

9 files changed

+337
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-06 22:33:40
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-06 22:36:46
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
1010
13+
1001
14+
0101
15+
0011
16+
*/
17+
typedef long long ll;
18+
class Solution {
19+
public:
20+
ll minimumSteps(string s) {
21+
ll ans = 0, times = 0;
22+
for (char c : s) {
23+
if (c == '1') {
24+
times++;
25+
}
26+
else {
27+
ans += times;
28+
}
29+
}
30+
return ans;
31+
}
32+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-06 22:44:04
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-06 22:44:22
6+
*/
7+
package main
8+
9+
func minimumSteps(s string) int64 {
10+
ans, times := int64(0), 0
11+
for _, c := range s {
12+
if c == '1' {
13+
times++
14+
} else { // 必须和上一个大括号写在同一行
15+
ans += int64(times)
16+
}
17+
}
18+
return ans
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-06 22:41:50
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-06 22:43:17
6+
*/
7+
class Solution {
8+
public long minimumSteps(String s) {
9+
long ans = 0, times = 0;
10+
for (char c : s.toCharArray()) {
11+
if (c == '1') {
12+
times++;
13+
}
14+
else {
15+
ans += times;
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-06 22:40:13
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-06 22:40:50
6+
'''
7+
class Solution:
8+
def minimumSteps(self, s: str) -> int:
9+
ans, times = 0, 0
10+
for c in s:
11+
if c == '1':
12+
times += 1
13+
else:
14+
ans += times
15+
return ans
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-05 13:24:58
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-05 13:56:36
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class MyTree {
12+
private:
13+
vector<int> a;
14+
15+
public:
16+
MyTree(int n) : a(n + 1) {}
17+
18+
void add(int n) {
19+
while (n < a.size()) {
20+
a[n]++;
21+
n += n & -n;
22+
}
23+
}
24+
25+
int get(int n) {
26+
int ans = 0;
27+
while (n > 0) {
28+
ans += a[n];
29+
n -= n & -n;
30+
}
31+
return ans;
32+
}
33+
};
34+
35+
class Solution {
36+
public:
37+
vector<int> resultArray(vector<int>& nums) {
38+
// 离散化
39+
vector<int> sorted(nums);
40+
sort(sorted.begin(), sorted.end());
41+
int length = unique(sorted.begin(), sorted.end()) - sorted.begin();
42+
unordered_map<int, int> ma;
43+
for (int i = 0; i < length; i++) {
44+
ma[sorted[i]] = i + 1;
45+
}
46+
// begin
47+
MyTree treeA(length), treeB(length);
48+
treeA.add(ma[nums[0]]), treeB.add(ma[nums[1]]);
49+
vector<int> a = {nums[0]}, b = {nums[1]};
50+
for (int i = 2; i < nums.size(); i++) {
51+
int na = a.size() - treeA.get(ma[nums[i]]), nb = b.size() - treeB.get(ma[nums[i]]);
52+
if (na > nb) {
53+
a.push_back(nums[i]);
54+
treeA.add(ma[nums[i]]);
55+
}
56+
else if (na < nb) {
57+
b.push_back(nums[i]);
58+
treeB.add(ma[nums[i]]);
59+
}
60+
else if (a.size() > b.size()) {
61+
b.push_back(nums[i]);
62+
treeB.add(ma[nums[i]]);
63+
}
64+
else {
65+
a.push_back(nums[i]);
66+
treeA.add(ma[nums[i]]);
67+
}
68+
}
69+
a.insert(a.end(), b.begin(), b.end());
70+
return a;
71+
}
72+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@
617617
|2923.找到冠军I|简单|<a href="https://leetcode.cn/problems/find-champion-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/12/LeetCode%202923.%E6%89%BE%E5%88%B0%E5%86%A0%E5%86%9BI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137678593" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-champion-i/solutions/2736315/letmefly-2923zhao-dao-guan-jun-ion2he-on-jrxw/" target="_blank">LeetCode题解</a>|
618618
|2924.找到冠军II|中等|<a href="https://leetcode.cn/problems/find-champion-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/13/LeetCode%202924.%E6%89%BE%E5%88%B0%E5%86%A0%E5%86%9BII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137707389" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-champion-ii/solutions/2737408/letmefly-2924zhao-dao-guan-jun-iinao-jin-miks/" target="_blank">LeetCode题解</a>|
619619
|2928.给小朋友们分糖果I|简单|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/01/LeetCode%202928.%E7%BB%99%E5%B0%8F%E6%9C%8B%E5%8F%8B%E4%BB%AC%E5%88%86%E7%B3%96%E6%9E%9CI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139380754" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/solutions/2797856/letmefly-2928gei-xiao-peng-you-men-fen-t-u01r/" target="_blank">LeetCode题解</a>|
620+
|2938.区分黑球与白球|中等|<a href="https://leetcode.cn/problems/separate-black-and-white-balls/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/06/LeetCode%202938.%E5%8C%BA%E5%88%86%E9%BB%91%E7%90%83%E4%B8%8E%E7%99%BD%E7%90%83/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139511813" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/separate-black-and-white-balls/solutions/2803386/letmefly-2938qu-fen-hei-qiu-yu-bai-qiu-h-6w6y/" target="_blank">LeetCode题解</a>|
620621
|2951.找出峰值|简单|<a href="https://leetcode.cn/problems/find-the-peaks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/28/LeetCode%202951.%E6%89%BE%E5%87%BA%E5%B3%B0%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139279605" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-peaks/solutions/2793708/letmefly-2951zhao-chu-feng-zhi-mo-ni-bia-5jn1/" target="_blank">LeetCode题解</a>|
621622
|2952.需要添加的硬币的最小数量|中等|<a href="https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/30/LeetCode%202952.%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E7%A1%AC%E5%B8%81%E7%9A%84%E6%9C%80%E5%B0%8F%E6%95%B0%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137185903" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/solutions/2716158/letmefly-2952xu-yao-tian-jia-de-ying-bi-bxfa4/" target="_blank">LeetCode题解</a>|
622623
|2960.统计已测试设备|简单|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/10/LeetCode%202960.%E7%BB%9F%E8%AE%A1%E5%B7%B2%E6%B5%8B%E8%AF%95%E8%AE%BE%E5%A4%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138672383" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/2772739/letmefly-2960tong-ji-yi-ce-shi-she-bei-k-59qt/" target="_blank">LeetCode题解</a>|
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: 2938.区分黑球与白球
3+
date: 2024-06-06 22:51:23
4+
tags: [题解, LeetCode, 中等, 贪心, 双指针, 字符串]
5+
---
6+
7+
# 【LetMeFly】2938.区分黑球与白球:黑球要与后面每个白球交换一次
8+
9+
力扣题目链接:[https://leetcode.cn/problems/separate-black-and-white-balls/](https://leetcode.cn/problems/separate-black-and-white-balls/)
10+
11+
<p>桌子上有 <code>n</code> 个球,每个球的颜色不是黑色,就是白色。</p>
12+
13+
<p>给你一个长度为 <code>n</code> 、下标从 <strong>0</strong> 开始的二进制字符串 <code>s</code>,其中 <code>1</code> 和 <code>0</code> 分别代表黑色和白色的球。</p>
14+
15+
<p>在每一步中,你可以选择两个相邻的球并交换它们。</p>
16+
17+
<p>返回「将所有黑色球都移到右侧,所有白色球都移到左侧所需的 <strong>最小步数</strong>」。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>s = "101"
25+
<strong>输出:</strong>1
26+
<strong>解释:</strong>我们可以按以下方式将所有黑色球移到右侧:
27+
- 交换 s[0] 和 s[1],s = "011"。
28+
最开始,1 没有都在右侧,需要至少 1 步将其移到右侧。</pre>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>s = "100"
34+
<strong>输出:</strong>2
35+
<strong>解释:</strong>我们可以按以下方式将所有黑色球移到右侧:
36+
- 交换 s[0] 和 s[1],s = "010"。
37+
- 交换 s[1] 和 s[2],s = "001"。
38+
可以证明所需的最小步数为 2 。
39+
</pre>
40+
41+
<p><strong class="example">示例 3:</strong></p>
42+
43+
<pre>
44+
<strong>输入:</strong>s = "0111"
45+
<strong>输出:</strong>0
46+
<strong>解释:</strong>所有黑色球都已经在右侧。
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>s[i]</code> 不是 <code>'0'</code>,就是 <code>'1'</code>。</li>
56+
</ul>
57+
58+
59+
60+
## 解题方法:一次遍历
61+
62+
同色球交换无意义,因此所有的交换都是:前面的黑球与黑球后面的白球。
63+
64+
因此统计一下每个黑球后面有多少个白球即可。
65+
66+
怎么统计?使用一个变量记录当前遍历到的黑球数,遇到一个黑球就黑球数加一,遇到一个白球答案就加上黑球数。
67+
68+
+ 时间复杂度$O(len(s))$
69+
+ 空间复杂度$O(1)$
70+
71+
### AC代码
72+
73+
#### C++
74+
75+
```cpp
76+
/*
77+
1010
78+
1001
79+
0101
80+
0011
81+
*/
82+
typedef long long ll;
83+
class Solution {
84+
public:
85+
ll minimumSteps(string s) {
86+
ll ans = 0, times = 0;
87+
for (char c : s) {
88+
if (c == '1') {
89+
times++;
90+
}
91+
else {
92+
ans += times;
93+
}
94+
}
95+
return ans;
96+
}
97+
};
98+
```
99+
100+
#### Go
101+
102+
```go
103+
// package main
104+
105+
func minimumSteps(s string) int64 {
106+
ans, times := int64(0), 0
107+
for _, c := range s {
108+
if c == '1' {
109+
times++
110+
} else { // 必须和上一个大括号写在同一行
111+
ans += int64(times)
112+
}
113+
}
114+
return ans
115+
}
116+
```
117+
118+
#### Java
119+
120+
```java
121+
class Solution {
122+
public long minimumSteps(String s) {
123+
long ans = 0, times = 0;
124+
for (char c : s.toCharArray()) {
125+
if (c == '1') {
126+
times++;
127+
}
128+
else {
129+
ans += times;
130+
}
131+
}
132+
return ans;
133+
}
134+
}
135+
```
136+
137+
#### Python
138+
139+
```python
140+
class Solution:
141+
def minimumSteps(self, s: str) -> int:
142+
ans, times = 0, 0
143+
for c in s:
144+
if c == '1':
145+
times += 1
146+
else:
147+
ans += times
148+
return ans
149+
```
150+
151+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/06/LeetCode%202938.%E5%8C%BA%E5%88%86%E9%BB%91%E7%90%83%E4%B8%8E%E7%99%BD%E7%90%83/)哦~
152+
>
153+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/139511813](https://letmefly.blog.csdn.net/article/details/139511813)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ tags: [其他, 知识, 英语, Notes]
220220
|sink|v. (使)下沉,沉没,降低,挖,掘,把...埋入,使受挫,使陷入麻烦<br/>n. 洗碗槽,洗脸盆<details><summary>过去式</summary>sank</details>|
221221
|||
222222
|ambassador|b. 大使|
223-
|villa|n. 别墅,豪宅|
223+
|<font color="#28bea0" title="二次复习">villa</font>|n. 别墅,豪宅|
224224
|fig|n. 【植】无花果,【植】无花果树<br/>abbr. (=figure)图,图表|
225225
|||
226226
|rebate|n. 折扣,退还款<br/>v. 退还(部分付款),扣除|
@@ -235,6 +235,17 @@ tags: [其他, 知识, 英语, Notes]
235235
|stencil|n. (印刷用)模板,(模板印的)文字<br/>v. 用模板印(文字/图案)|
236236
|pluck|v. 摘,拔(去),拨(弦),解救v. 胆识,胆量,内脏,快而猛的拉,勇气|
237237
|daybreak|n. 黎明,破晓|
238+
|||
239+
|adorn|v. 装饰,装扮|
240+
|||
241+
|reef|n. 礁,礁脉,帆的可收缩部<br/>v. 收帆,卷起缩帆部,叠起缩帆部|
242+
|weld|v. 焊接,熔接,使紧密结合<br/>n. 焊接点,焊接处|
243+
|hydrogen|n. 氢,氢气|
244+
|smash|n. 打碎,破碎(声),走红的作品<br/>v. 打碎,猛撞,猛击,击溃,粉碎|
245+
|materialize|v. 实现,发生,成为现实,突然显现|
246+
|serene|adj. 平静的,宁静的,安详的<br/>v. 宁静,<诗>使(海,脸色等)平静,使(天空)明朗<br/>n. 平静(的海),<诗,古>晴朗(天空)|
247+
|hazard|n. 危险,危害<br/>冒失地提出,冒险猜测,冒...的风险,使处于危险|
248+
238249

239250
<details>
240251
<summary>扇贝音频测试</summary>

0 commit comments

Comments
 (0)