|
1 | 1 | package g3301_3400.s3399_smallest_substring_with_identical_characters_ii; |
2 | 2 |
|
3 | | -// #Hard #2024_12_24_Time_11_ms_(100.00%)_Space_45.7_MB_(54.55%) |
| 3 | +// #Hard #2024_12_24_Time_15_ms_(99.39%)_Space_45.9_MB_(43.03%) |
4 | 4 |
|
5 | 5 | import java.util.ArrayList; |
6 | 6 | import java.util.List; |
7 | 7 |
|
8 | 8 | public class Solution { |
9 | 9 | public int minLength(String s, int numOps) { |
10 | | - int l = s.length(); |
11 | | - int lingyi = 0; |
12 | | - int yiling = 0; |
13 | | - List<Integer> pq = new ArrayList<>(); |
14 | | - char thisone = s.charAt(0); |
15 | | - int chang = 1; |
16 | | - if (thisone == '0') { |
17 | | - yiling++; |
18 | | - } else { |
19 | | - lingyi++; |
20 | | - } |
21 | | - for (int i = 1; i < l; i++) { |
22 | | - char cur = s.charAt(i); |
23 | | - if (cur == thisone) { |
24 | | - chang++; |
25 | | - } else { |
26 | | - if (chang >= 2) { |
27 | | - pq.add(chang); |
28 | | - } |
29 | | - chang = 1; |
30 | | - thisone = cur; |
| 10 | + byte[] b = s.getBytes(); |
| 11 | + int flips1 = 0; |
| 12 | + int flips2 = 0; |
| 13 | + for (int i = 0; i < b.length; i++) { |
| 14 | + byte e1 = (byte) ((i % 2 == 0) ? '0' : '1'); |
| 15 | + byte e2 = (byte) ((i % 2 == 0) ? '1' : '0'); |
| 16 | + if (b[i] != e1) { |
| 17 | + flips1++; |
31 | 18 | } |
32 | | - if (i % 2 == 0) { |
33 | | - if (cur == '0') { |
34 | | - yiling++; |
35 | | - } else { |
36 | | - lingyi++; |
37 | | - } |
38 | | - } else { |
39 | | - if (cur == '0') { |
40 | | - lingyi++; |
41 | | - } else { |
42 | | - yiling++; |
43 | | - } |
| 19 | + if (b[i] != e2) { |
| 20 | + flips2++; |
44 | 21 | } |
45 | 22 | } |
46 | | - if (numOps >= lingyi || numOps >= yiling) { |
| 23 | + int flips = Math.min(flips1, flips2); |
| 24 | + if (flips <= numOps) { |
47 | 25 | return 1; |
48 | 26 | } |
49 | | - if (chang >= 2) { |
50 | | - pq.add(chang); |
51 | | - } |
52 | | - int one = -1; |
53 | | - int two = -1; |
54 | | - for (int cur : pq) { |
55 | | - if (cur > one) { |
56 | | - two = one; |
57 | | - one = cur; |
58 | | - } else if (cur > two) { |
59 | | - two = cur; |
| 27 | + List<Integer> seg = new ArrayList<>(); |
| 28 | + int count = 1; |
| 29 | + int max = 1; |
| 30 | + for (int i = 1; i < b.length; i++) { |
| 31 | + if (b[i] != b[i - 1]) { |
| 32 | + if (count != 1) { |
| 33 | + seg.add(count); |
| 34 | + max = Math.max(max, count); |
| 35 | + } |
| 36 | + count = 1; |
| 37 | + } else { |
| 38 | + count++; |
60 | 39 | } |
61 | 40 | } |
62 | | - if (two == -1) { |
63 | | - return one / (numOps + 1) > 1 ? one / (numOps + 1) : 2; |
64 | | - } |
65 | | - if (numOps == 0) { |
66 | | - return one; |
| 41 | + if (count != 1) { |
| 42 | + seg.add(count); |
| 43 | + max = Math.max(max, count); |
67 | 44 | } |
68 | | - if (numOps == 1) { |
69 | | - return (one / 2 > two) ? (one / 2 == 1 ? 2 : one / 2) : two; |
| 45 | + int l = 2; |
| 46 | + int r = max; |
| 47 | + int res = max; |
| 48 | + while (l <= r) { |
| 49 | + int m = l + (r - l) / 2; |
| 50 | + if (check(m, seg, numOps)) { |
| 51 | + r = m - 1; |
| 52 | + res = m; |
| 53 | + } else { |
| 54 | + l = m + 1; |
| 55 | + } |
70 | 56 | } |
71 | | - int left = 2; |
72 | | - int right = l / (numOps + 1); |
73 | | - while (left < right) { |
74 | | - int mid = left + (right - left) / 2; |
75 | | - int sum = 0; |
76 | | - for (Integer integer : pq) { |
77 | | - sum += integer / (mid + 1); |
| 57 | + return res; |
| 58 | + } |
| 59 | + |
| 60 | + private boolean check(int sz, List<Integer> seg, int ops) { |
| 61 | + for (int i : seg) { |
| 62 | + if (i <= sz) { |
| 63 | + continue; |
78 | 64 | } |
79 | | - if (sum <= numOps) { |
80 | | - right = mid; |
81 | | - } else { |
82 | | - left = mid + 1; |
| 65 | + int x = i / (sz + 1); |
| 66 | + ops -= x; |
| 67 | + if (ops < 0) { |
| 68 | + return false; |
83 | 69 | } |
84 | 70 | } |
85 | | - return left; |
| 71 | + return true; |
86 | 72 | } |
87 | 73 | } |
0 commit comments