Skip to content

Commit 3105af0

Browse files
committed
一刷926
1 parent d671a13 commit 3105af0

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6507,14 +6507,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
65076507
//|{doc_base_url}/0925-long-pressed-name.adoc[题解]
65086508
//|Easy
65096509
//|
6510-
//
6511-
//|{counter:codes}
6512-
//|{leetcode_base_url}/flip-string-to-monotone-increasing/[926. Flip String to Monotone Increasing^]
6513-
//|{source_base_url}/_0926_FlipStringToMonotoneIncreasing.java[Java]
6514-
//|{doc_base_url}/0926-flip-string-to-monotone-increasing.adoc[题解]
6515-
//|Medium
6516-
//|
6517-
//
6510+
6511+
|{counter:codes}
6512+
|{leetcode_base_url}/flip-string-to-monotone-increasing/[926. Flip String to Monotone Increasing^]
6513+
|{source_base_url}/_0926_FlipStringToMonotoneIncreasing.java[Java]
6514+
|{doc_base_url}/0926-flip-string-to-monotone-increasing.adoc[题解]
6515+
|Medium
6516+
|
6517+
65186518
//|{counter:codes}
65196519
//|{leetcode_base_url}/three-equal-parts/[927. Three Equal Parts^]
65206520
//|{source_base_url}/_0927_ThreeEqualParts.java[Java]

docs/0926-flip-string-to-monotone-increasing.adoc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,35 @@ Return the minimum number of flips to make `S` monotone increasing.
5050
. `S` only consists of `'0'` and `'1'` characters.
5151

5252

53+
== 思路分析
5354

55+
如果下标 i 处的字符是 0,则只有当下标 i−1 处的字符是 0 时才符合单调递增;如果下标 i 处的字符是 1,则下标 i−1 处的字符是 0 或 1 都符合单调递增,此时为了将翻转次数最小化,应分别考虑下标 i−1 处的字符是 0 和 1 的情况下需要的翻转次数,取两者的最小值。
5456

55-
57+
对于 0≤i<n,用 dp[i][0] 和 dp[i][1] 分别表示下标 i 处的字符为 0 和 1 的情况下使得 s[0..i] 单调递增的最小翻转次数。
5658

5759
[[src-0926]]
60+
[tabs]
61+
====
62+
一刷::
63+
+
64+
--
5865
[{java_src_attr}]
5966
----
6067
include::{sourcedir}/_0926_FlipStringToMonotoneIncreasing.java[tag=answer]
6168
----
69+
--
70+
71+
// 二刷::
72+
// +
73+
// --
74+
// [{java_src_attr}]
75+
// ----
76+
// include::{sourcedir}/_0926_FlipStringToMonotoneIncreasing_2.java[tag=answer]
77+
// ----
78+
// --
79+
====
80+
81+
== 参考资料
82+
83+
. https://leetcode.cn/problems/flip-string-to-monotone-increasing/solutions/1592230/jiang-zi-fu-chuan-fan-zhuan-dao-dan-diao-stjd/[926. 将字符串翻转到单调递增 - 官方题解^]
6284

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,7 @@ include::0912-sort-an-array.adoc[leveloffset=+1]
19261926

19271927
// include::0925-long-pressed-name.adoc[leveloffset=+1]
19281928

1929-
// include::0926-flip-string-to-monotone-increasing.adoc[leveloffset=+1]
1929+
include::0926-flip-string-to-monotone-increasing.adoc[leveloffset=+1]
19301930

19311931
// include::0927-three-equal-parts.adoc[leveloffset=+1]
19321932

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,11 @@
858858
|{doc_base_url}/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
859859
|❌ 贪心算法
860860

861+
|{counter:codes}
862+
|{leetcode_base_url}/flip-string-to-monotone-increasing/[926. Flip String to Monotone Increasing^]
863+
|{doc_base_url}/0926-flip-string-to-monotone-increasing.adoc[题解]
864+
|❌ 动态规划
865+
861866
|===
862867

863868
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0926_FlipStringToMonotoneIncreasing {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-24 17:54:37
8+
*/
9+
public int minFlipsMonoIncr(String s) {
10+
// 1. 确定 dp 数组(dp table)以及下标的含义
11+
// 对于 0≤i<n,用 dp[i][0] 和 dp[i][1] 分别表示下标 i 处的
12+
// 字符为 0 和 1 的情况下使得 s[0..i] 单调递增的最小翻转次数。
13+
// 如果下标 i 处的字符是 0,则只有当下标 i−1 处的字符是 0 时才符合
14+
// 单调递增;如果下标 i 处的字符是 1,则下标 i−1 处的字符是 0 或 1
15+
// 都符合单调递增,此时为了将翻转次数最小化,应分别考虑下标 i−1 处的
16+
// 字符是 0 和 1 的情况下需要的翻转次数,取两者的最小值。
17+
int[][] dp = new int[s.length() + 1][2];
18+
// 3. dp 数组如何初始化
19+
dp[0][0] = 0;
20+
dp[0][1] = 0;
21+
// 4. 确定遍历顺序
22+
for (int i = 1; i <= s.length(); i++) {
23+
boolean isZero = s.charAt(i - 1) == '0';
24+
// 2. 确定递推公式
25+
dp[i][0] = dp[i - 1][0] + (!isZero ? 1 : 0);
26+
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][1]) + (isZero ? 1 : 0);
27+
}
28+
return Math.min(dp[s.length()][0], dp[s.length()][1]);
29+
}
30+
// end::answer[]
31+
}

0 commit comments

Comments
 (0)