Skip to content

Commit d671a13

Browse files
committed
一刷1864
1 parent 5d9402b commit d671a13

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13074,12 +13074,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1307413074
//|Easy
1307513075
//|
1307613076

13077-
//|{counter:codes}
13078-
//|{leetcode_base_url}/minimum-number-of-swaps-to-make-the-binary-string-alternating/[1864. Minimum Number of Swaps to Make the Binary String Alternating^]
13079-
//|{source_base_url}/_1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating.java[Java]
13080-
//|{doc_base_url}/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
13081-
//|Medium
13082-
//|
13077+
|{counter:codes}
13078+
|{leetcode_base_url}/minimum-number-of-swaps-to-make-the-binary-string-alternating/[1864. Minimum Number of Swaps to Make the Binary String Alternating^]
13079+
|{source_base_url}/_1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating.java[Java]
13080+
|{doc_base_url}/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
13081+
|Medium
13082+
|
1308313083

1308413084
//|{counter:codes}
1308513085
//|{leetcode_base_url}/finding-pairs-with-a-certain-sum/[1865. Finding Pairs With a Certain Sum^]

docs/0000-00-note.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ image::images/quick-sort-01.gif[{image_attr}]
249249

250250
== 参考资料
251251

252+
. https://leetcode.cn/circle/discuss/RvFUtj/[如何科学刷题? - 按照难度系数刷题^]
252253
. https://www.designgurus.io/course/grokking-the-coding-interview[Grokking the Coding Interview Patterns^]
253254
. https://leetcode.ca/all/1650.html[Leetcode 1650. Lowest Common Ancestor of a Binary Tree III^] -- 会员题目可以在这里查找。
254255
. https://github.com/doocs/leetcode[doocs/leetcode: 🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解^] -- 也可以在这里找题目

docs/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ The string is now alternating.
4141
*Constraints:*
4242

4343

44-
* `1 <= s.length <= 1000`
44+
* `1 \<= s.length \<= 1000`
4545
* `s[i]` is either `'0'` or `'1'`.
4646

4747

4848

4949

5050
== 思路分析
5151

52+
如果是交替出现,那么只有 `010...` 或者 `101...` 这两种类型,统计各个比特位不正确的位数再除以 2 即可。(交换一次,可以解决两个不正确比特位)。
5253

5354
[[src-1864]]
5455
[tabs]
@@ -75,4 +76,5 @@ include::{sourcedir}/_1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating.
7576

7677
== 参考资料
7778

79+
. https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/solutions/779087/minimum-number-of-swaps-to-make-the-bina-z0qy/[1864. 构成交替字符串需要的最小交换次数 - 官方题解^]
7880

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3802,7 +3802,7 @@ include::1823-find-the-winner-of-the-circular-game.adoc[leveloffset=+1]
38023802

38033803
// include::1863-sum-of-all-subset-xor-totals.adoc[leveloffset=+1]
38043804

3805-
// include::1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[leveloffset=+1]
3805+
include::1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[leveloffset=+1]
38063806

38073807
// include::1865-finding-pairs-with-a-certain-sum.adoc[leveloffset=+1]
38083808

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,11 @@
853853
|{doc_base_url}/0079-word-search.adoc[题解]
854854
|✅ 回溯
855855

856+
|{counter:codes}
857+
|{leetcode_base_url}/minimum-number-of-swaps-to-make-the-binary-string-alternating/[1864. Minimum Number of Swaps to Make the Binary String Alternating^]
858+
|{doc_base_url}/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
859+
|❌ 贪心算法
860+
856861
|===
857862

858863
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2024-09-24 16:26:07
9+
*/
10+
public int minSwaps(String s) {
11+
int[] bits = bits(s);
12+
int b0 = bits[0];
13+
int b1 = bits[1];
14+
int n = s.length();
15+
int result = Integer.MAX_VALUE;
16+
// 1010...
17+
if (b0 == n / 2 && b1 == (n + 1) / 2) {
18+
int diff = 0;
19+
for (int i = 0; i < n; i++) {
20+
if (s.charAt(i) - '0' == i % 2) {
21+
diff++;
22+
}
23+
}
24+
result = Math.min(result, diff / 2);
25+
}
26+
// 0101...
27+
if (b0 == (n + 1) / 2 && b1 == n / 2) {
28+
int diff = 0;
29+
for (int i = 0; i < n; i++) {
30+
if (s.charAt(i) - '0' != i % 2) {
31+
diff++;
32+
}
33+
}
34+
result = Math.min(result, diff / 2);
35+
}
36+
return result != Integer.MAX_VALUE ? result : -1;
37+
}
38+
39+
private int[] bits(String s) {
40+
int b0 = 0;
41+
for (int i = 0; i < s.length(); i++) {
42+
if (s.charAt(i) == '0') {
43+
b0++;
44+
}
45+
}
46+
return new int[]{b0, s.length() - b0};
47+
}
48+
49+
// end::answer[]
50+
51+
public static void main(String[] args) {
52+
new _1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating()
53+
.minSwaps("1110");
54+
}
55+
}

0 commit comments

Comments
 (0)