-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3399-SmallestSubstringWithIdenticalCharactersII.go
More file actions
155 lines (142 loc) · 3.87 KB
/
3399-SmallestSubstringWithIdenticalCharactersII.go
File metadata and controls
155 lines (142 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
// 3399. Smallest Substring With Identical Characters II
// You are given a binary string s of length n and an integer numOps.
// You are allowed to perform the following operation on s at most numOps times:
// 1. Select any index i (where 0 <= i < n) and flip s[i].
// If s[i] == '1', change s[i] to '0' and vice versa.
// You need to minimize the length of the longest substring of s such that all the characters in the substring are identical.
// Return the minimum length after the operations.
// Example 1:
// Input: s = "000001", numOps = 1
// Output: 2
// Explanation:
// By changing s[2] to '1', s becomes "001001". The longest substrings with identical characters are s[0..1] and s[3..4].
// Example 2:
// Input: s = "0000", numOps = 2
// Output: 1
// Explanation:
// By changing s[0] and s[2] to '1', s becomes "1010".
// Example 3:
// Input: s = "0101", numOps = 0
// Output: 1
// Constraints:
// 1 <= n == s.length <= 10^5
// s consists only of '0' and '1'.
// 0 <= numOps <= n
import "fmt"
func minLength(s string, numOps int) int {
n := len(s)
if n == 1 { return 1 }
helper := func(s string, mx int) int {
n := len(s)
if mx == 1 {
a, b := 0, 0
for i := 0; i < n; i++ {
if s[i] == '0' {
a, b = b, a + 1
} else {
a, b = b + 1, a
}
}
if b < a {
return b
}
return a
}
p, c, res := s[0], 0, 0
for i := 0; i < n; i++ {
if s[i] == p {
c++
continue
}
if c > mx {
res += c / (mx + 1)
}
p, c = s[i], 1
}
if c > mx {
res += c / (mx + 1)
}
return res
}
l, r := 0, n
for r - l > 1 {
m := (l + r) / 2
if helper(s, m) > numOps {
l = m
} else {
r = m
}
}
return r
}
func minLength1(s string, numOps int) int {
count, arr := 0, []int{}
for i, j := 0, 0; i < len(s); i++ {
if s[i] != s[j] {
arr = append(arr, count)
count, j = 0, i
}
count++
}
arr = append(arr, count)
check0 := func(st int) bool {
count := numOps
for _, v := range s {
if byte(v) != ('0') + byte(st) {
count--
}
st = st ^ 1
}
return count >= 0
}
check := func(dist int) bool {
if dist == 1 { // 1001 无解 10001 ok 100001
return check0(0) || check0(1)
}
count := numOps
for _, v := range arr {
if v <= dist { // 不需要操作
continue
}
for ; v > dist; v -= (dist + 1) {
count--
}
if count < 0 {
return false
}
}
return count >= 0
}
l, r := 1, len(s)
for l < r {
mid := (l + r) / 2
if check(mid) {
r = mid
} else {
l = mid + 1
}
}
return r
}
func main() {
// Example 1:
// Input: s = "000001", numOps = 1
// Output: 2
// Explanation:
// By changing s[2] to '1', s becomes "001001". The longest substrings with identical characters are s[0..1] and s[3..4].
fmt.Println(minLength("000001", 1)) // 2
// Example 2:
// Input: s = "0000", numOps = 2
// Output: 1
// Explanation:
// By changing s[0] and s[2] to '1', s becomes "1010".
fmt.Println(minLength("0000", 2)) // 1
// Example 3:
// Input: s = "0101", numOps = 0
// Output: 1
fmt.Println(minLength("0101", 0)) // 1
fmt.Println(minLength1("000001", 1)) // 2
fmt.Println(minLength1("0000", 2)) // 1
fmt.Println(minLength1("0101", 0)) // 1
}