|
62 | 62 |
|
63 | 63 | <!-- 这里可写通用的实现逻辑 -->
|
64 | 64 |
|
| 65 | +思路同 [1004. 最大连续 1 的个数 III](/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README.md) |
| 66 | + |
| 67 | +维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为要求的是”最大“,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。 |
| 68 | + |
| 69 | +- l 是窗口左端点,负责移动起始位置 |
| 70 | +- r 是窗口右端点,负责扩展窗口 |
| 71 | +- k 是资源数,每次要替换,k 减 1,同时 r 向右移动 |
| 72 | +- `r++` 每次都会执行,`l++` 只有资源 `k < 0` 时才触发,因此 `r - l` 的值只会单调递增(或保持不变) |
| 73 | +- 移动左端点时,如果可以释放一个资源,k 加 1 |
| 74 | + |
65 | 75 | <!-- tabs:start -->
|
66 | 76 |
|
67 | 77 | ### **Python3**
|
68 | 78 |
|
69 | 79 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
70 | 80 |
|
71 | 81 | ```python
|
72 |
| - |
| 82 | +class Solution: |
| 83 | + def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int: |
| 84 | + def get(c, k): |
| 85 | + l = r = -1 |
| 86 | + while r < len(answerKey) - 1: |
| 87 | + r += 1 |
| 88 | + if answerKey[r] == c: |
| 89 | + k -= 1 |
| 90 | + if k < 0: |
| 91 | + l += 1 |
| 92 | + if answerKey[l] == c: |
| 93 | + k += 1 |
| 94 | + return r - l |
| 95 | + |
| 96 | + return max(get('T', k), get('F', k)) |
73 | 97 | ```
|
74 | 98 |
|
75 | 99 | ### **Java**
|
76 | 100 |
|
77 | 101 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
78 | 102 |
|
79 | 103 | ```java
|
| 104 | +class Solution { |
| 105 | + public int maxConsecutiveAnswers(String answerKey, int k) { |
| 106 | + return Math.max(get('T', k, answerKey), get('F', k, answerKey)); |
| 107 | + } |
| 108 | + |
| 109 | + public int get(char c, int k, String answerKey) { |
| 110 | + int l = 0, r = 0; |
| 111 | + while (r < answerKey.length()) { |
| 112 | + if (answerKey.charAt(r++) == c) { |
| 113 | + --k; |
| 114 | + } |
| 115 | + if (k < 0 && answerKey.charAt(l++) == c) { |
| 116 | + ++k; |
| 117 | + } |
| 118 | + } |
| 119 | + return r - l; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### **C++** |
| 125 | + |
| 126 | +```cpp |
| 127 | +class Solution { |
| 128 | +public: |
| 129 | + int maxConsecutiveAnswers(string answerKey, int k) { |
| 130 | + return max(get('T', k, answerKey), get('F', k, answerKey)); |
| 131 | + } |
| 132 | + |
| 133 | + int get(char c, int k, string answerKey) { |
| 134 | + int l = 0, r = 0; |
| 135 | + while (r < answerKey.size()) |
| 136 | + { |
| 137 | + if (answerKey[r++] == c) --k; |
| 138 | + if (k < 0 && answerKey[l++] == c) ++k; |
| 139 | + } |
| 140 | + return r - l; |
| 141 | + } |
| 142 | +}; |
| 143 | +``` |
80 | 144 |
|
| 145 | +### **Go** |
| 146 | + |
| 147 | +```go |
| 148 | +func maxConsecutiveAnswers(answerKey string, k int) int { |
| 149 | + get := func(c byte, k int) int { |
| 150 | + l, r := -1, -1 |
| 151 | + for r < len(answerKey)-1 { |
| 152 | + r++ |
| 153 | + if answerKey[r] == c { |
| 154 | + k-- |
| 155 | + } |
| 156 | + if k < 0 { |
| 157 | + l++ |
| 158 | + if answerKey[l] == c { |
| 159 | + k++ |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + return r - l |
| 164 | + } |
| 165 | + return max(get('T', k), get('F', k)) |
| 166 | +} |
| 167 | + |
| 168 | +func max(a, b int) int { |
| 169 | + if a > b { |
| 170 | + return a |
| 171 | + } |
| 172 | + return b |
| 173 | +} |
81 | 174 | ```
|
82 | 175 |
|
83 | 176 | ### **...**
|
|
0 commit comments