Skip to content

Commit cd71a68

Browse files
committed
feat: add solutions to lc problems: No.1004,2024
* No.1004.Max Consecutive Ones III * No.2024.Maximize the Confusion of an Exam
1 parent e36ae40 commit cd71a68

File tree

12 files changed

+438
-11
lines changed

12 files changed

+438
-11
lines changed

solution/1000-1099/1004.Max Consecutive Ones III/README.md

+73-2
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,94 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
思路同 [2024. 考试的最大困扰度](/solution/2000-2099/2024.Maximize%20the%20Confusion%20of%20an%20Exam/README.md)
46+
47+
维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为要求的是”最大“,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。
48+
49+
- l 是窗口左端点,负责移动起始位置
50+
- r 是窗口右端点,负责扩展窗口
51+
- k 是资源数,每次要替换 0,k 减 1,同时 r 向右移动
52+
- `r++` 每次都会执行,`l++` 只有资源 `k < 0` 时才触发,因此 `r - l` 的值只会单调递增(或保持不变)
53+
- 移动左端点时,如果当前元素是 0,说明可以释放一个资源,k 加 1
54+
4555
<!-- tabs:start -->
4656

4757
### **Python3**
4858

4959
<!-- 这里可写当前语言的特殊实现逻辑 -->
5060

5161
```python
52-
62+
class Solution:
63+
def longestOnes(self, nums: List[int], k: int) -> int:
64+
l = r = -1
65+
while r < len(nums) - 1:
66+
r += 1
67+
if nums[r] == 0:
68+
k -= 1
69+
if k < 0:
70+
l += 1
71+
if nums[l] == 0:
72+
k += 1
73+
return r - l
5374
```
5475

5576
### **Java**
5677

5778
<!-- 这里可写当前语言的特殊实现逻辑 -->
5879

5980
```java
81+
class Solution {
82+
public int longestOnes(int[] nums, int k) {
83+
int l = 0, r = 0;
84+
while (r < nums.length) {
85+
if (nums[r++] == 0) {
86+
--k;
87+
}
88+
if (k < 0 && nums[l++] == 0) {
89+
++k;
90+
}
91+
}
92+
return r - l;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int longestOnes(vector<int>& nums, int k) {
103+
int l = 0, r = 0;
104+
while (r < nums.size())
105+
{
106+
if (nums[r++] == 0) --k;
107+
if (k < 0 && nums[l++] == 0) ++k;
108+
}
109+
return r - l;
110+
}
111+
};
112+
```
60113
114+
### **Go**
115+
116+
```go
117+
func longestOnes(nums []int, k int) int {
118+
l, r := -1, -1
119+
for r < len(nums)-1 {
120+
r++
121+
if nums[r] == 0 {
122+
k--
123+
}
124+
if k < 0 {
125+
l++
126+
if nums[l] == 0 {
127+
k++
128+
}
129+
}
130+
}
131+
return r - l
132+
}
61133
```
62134

63135
### **...**
@@ -67,4 +139,3 @@
67139
```
68140

69141
<!-- tabs:end -->
70-
<!-- tabs:end -->

solution/1000-1099/1004.Max Consecutive Ones III/README_EN.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,75 @@ Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
4040
### **Python3**
4141

4242
```python
43-
43+
class Solution:
44+
def longestOnes(self, nums: List[int], k: int) -> int:
45+
l = r = -1
46+
while r < len(nums) - 1:
47+
r += 1
48+
if nums[r] == 0:
49+
k -= 1
50+
if k < 0:
51+
l += 1
52+
if nums[l] == 0:
53+
k += 1
54+
return r - l
4455
```
4556

4657
### **Java**
4758

4859
```java
60+
class Solution {
61+
public int longestOnes(int[] nums, int k) {
62+
int l = 0, r = 0;
63+
while (r < nums.length) {
64+
if (nums[r++] == 0) {
65+
--k;
66+
}
67+
if (k < 0 && nums[l++] == 0) {
68+
++k;
69+
}
70+
}
71+
return r - l;
72+
}
73+
}
74+
```
4975

76+
### **C++**
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
int longestOnes(vector<int>& nums, int k) {
82+
int l = 0, r = 0;
83+
while (r < nums.size())
84+
{
85+
if (nums[r++] == 0) --k;
86+
if (k < 0 && nums[l++] == 0) ++k;
87+
}
88+
return r - l;
89+
}
90+
};
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
func longestOnes(nums []int, k int) int {
97+
l, r := -1, -1
98+
for r < len(nums)-1 {
99+
r++
100+
if nums[r] == 0 {
101+
k--
102+
}
103+
if k < 0 {
104+
l++
105+
if nums[l] == 0 {
106+
k++
107+
}
108+
}
109+
}
110+
return r - l
111+
}
50112
```
51113

52114
### **...**
@@ -56,4 +118,3 @@ Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
56118
```
57119

58120
<!-- tabs:end -->
59-
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int longestOnes(vector<int>& nums, int k) {
4+
int l = 0, r = 0;
5+
while (r < nums.size())
6+
{
7+
if (nums[r++] == 0) --k;
8+
if (k < 0 && nums[l++] == 0) ++k;
9+
}
10+
return r - l;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func longestOnes(nums []int, k int) int {
2+
l, r := -1, -1
3+
for r < len(nums)-1 {
4+
r++
5+
if nums[r] == 0 {
6+
k--
7+
}
8+
if k < 0 {
9+
l++
10+
if nums[l] == 0 {
11+
k++
12+
}
13+
}
14+
}
15+
return r - l
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
class Solution {
2-
public int longestOnes(int[] A, int K) {
2+
public int longestOnes(int[] nums, int k) {
33
int l = 0, r = 0;
4-
while (r < A.length) {
5-
if (A[r++] == 0) --K;
6-
if (K < 0 && A[l++] == 0) ++K;
4+
while (r < nums.length) {
5+
if (nums[r++] == 0) {
6+
--k;
7+
}
8+
if (k < 0 && nums[l++] == 0) {
9+
++k;
10+
}
711
}
812
return r - l;
913
}
10-
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def longestOnes(self, nums: List[int], k: int) -> int:
3+
l = r = -1
4+
while r < len(nums) - 1:
5+
r += 1
6+
if nums[r] == 0:
7+
k -= 1
8+
if k < 0:
9+
l += 1
10+
if nums[l] == 0:
11+
k += 1
12+
return r - l

solution/2000-2099/2024.Maximize the Confusion of an Exam/README.md

+94-1
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,115 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

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+
6575
<!-- tabs:start -->
6676

6777
### **Python3**
6878

6979
<!-- 这里可写当前语言的特殊实现逻辑 -->
7080

7181
```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))
7397
```
7498

7599
### **Java**
76100

77101
<!-- 这里可写当前语言的特殊实现逻辑 -->
78102

79103
```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+
```
80144

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+
}
81174
```
82175

83176
### **...**

0 commit comments

Comments
 (0)