File tree Expand file tree Collapse file tree 5 files changed +68
-10
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 5 files changed +68
-10
lines changed Original file line number Diff line number Diff line change @@ -6507,14 +6507,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
6507
6507
//|{doc_base_url}/0925-long-pressed-name.adoc[题解]
6508
6508
//|Easy
6509
6509
//|
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
+
6518
6518
//|{counter:codes}
6519
6519
//|{leetcode_base_url}/three-equal-parts/[927. Three Equal Parts^]
6520
6520
//|{source_base_url}/_0927_ThreeEqualParts.java[Java]
Original file line number Diff line number Diff line change @@ -50,13 +50,35 @@ Return the minimum number of flips to make `S` monotone increasing.
50
50
. `S` only consists of `'0'` and `'1'` characters.
51
51
52
52
53
+ == 思路分析
53
54
55
+ 如果下标 i 处的字符是 0,则只有当下标 i−1 处的字符是 0 时才符合单调递增;如果下标 i 处的字符是 1,则下标 i−1 处的字符是 0 或 1 都符合单调递增,此时为了将翻转次数最小化,应分别考虑下标 i−1 处的字符是 0 和 1 的情况下需要的翻转次数,取两者的最小值。
54
56
55
-
57
+ 对于 0≤i<n,用 dp[i][0] 和 dp[i][1] 分别表示下标 i 处的字符为 0 和 1 的情况下使得 s[0..i] 单调递增的最小翻转次数。
56
58
57
59
[[src-0926]]
60
+ [tabs]
61
+ ====
62
+ 一刷::
63
+ +
64
+ --
58
65
[{java_src_attr}]
59
66
----
60
67
include::{sourcedir}/_0926_FlipStringToMonotoneIncreasing.java[tag=answer]
61
68
----
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. 将字符串翻转到单调递增 - 官方题解^]
62
84
Original file line number Diff line number Diff line change @@ -1926,7 +1926,7 @@ include::0912-sort-an-array.adoc[leveloffset=+1]
1926
1926
1927
1927
// include::0925-long-pressed-name.adoc[leveloffset=+1]
1928
1928
1929
- // include::0926-flip-string-to-monotone-increasing.adoc[leveloffset=+1]
1929
+ include::0926-flip-string-to-monotone-increasing.adoc[leveloffset=+1]
1930
1930
1931
1931
// include::0927-three-equal-parts.adoc[leveloffset=+1]
1932
1932
Original file line number Diff line number Diff line change 858
858
|{doc_base_url} /1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
859
859
|❌ 贪心算法
860
860
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
+
861
866
|===
862
867
863
868
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments