From 1db6ffa9434544bf7f71615555de3b1c79f1abce Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 10 Jul 2024 13:08:50 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.0487,1004,2024 --- .../0487.Max Consecutive Ones II/README.md | 349 +++--------------- .../0487.Max Consecutive Ones II/README_EN.md | 323 +++------------- .../0487.Max Consecutive Ones II/Solution.cpp | 40 +- .../0487.Max Consecutive Ones II/Solution.go | 44 +-- .../Solution.java | 39 +- .../0487.Max Consecutive Ones II/Solution.js | 14 + .../0487.Max Consecutive Ones II/Solution.py | 31 +- .../0487.Max Consecutive Ones II/Solution.ts | 10 + .../Solution2.cpp | 19 - .../0487.Max Consecutive Ones II/Solution2.go | 17 - .../Solution2.java | 18 - .../0487.Max Consecutive Ones II/Solution2.py | 13 - .../Solution3.cpp | 16 - .../0487.Max Consecutive Ones II/Solution3.go | 16 - .../Solution3.java | 15 - .../0487.Max Consecutive Ones II/Solution3.py | 13 - .../1004.Max Consecutive Ones III/README.md | 228 ++---------- .../README_EN.md | 228 ++---------- .../Solution.cpp | 30 +- .../1004.Max Consecutive Ones III/Solution.go | 26 +- .../Solution.java | 28 +- .../1004.Max Consecutive Ones III/Solution.py | 22 +- .../1004.Max Consecutive Ones III/Solution.ts | 14 +- .../Solution2.cpp | 11 - .../Solution2.go | 16 - .../Solution2.java | 14 - .../Solution2.py | 12 - .../Solution2.rs | 18 - .../Solution2.ts | 13 - .../README.md | 105 +++--- .../README_EN.md | 107 +++--- .../Solution.cpp | 14 +- .../Solution.go | 15 +- .../Solution.java | 14 +- .../Solution.py | 14 +- .../Solution.rs | 24 +- .../Solution.ts | 13 +- 37 files changed, 446 insertions(+), 1497 deletions(-) create mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution.js create mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution.ts delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution2.cpp delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution2.go delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution2.java delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution2.py delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution3.cpp delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution3.go delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution3.java delete mode 100644 solution/0400-0499/0487.Max Consecutive Ones II/Solution3.py delete mode 100644 solution/1000-1099/1004.Max Consecutive Ones III/Solution2.cpp delete mode 100644 solution/1000-1099/1004.Max Consecutive Ones III/Solution2.go delete mode 100644 solution/1000-1099/1004.Max Consecutive Ones III/Solution2.java delete mode 100644 solution/1000-1099/1004.Max Consecutive Ones III/Solution2.py delete mode 100644 solution/1000-1099/1004.Max Consecutive Ones III/Solution2.rs delete mode 100644 solution/1000-1099/1004.Max Consecutive Ones III/Solution2.ts diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/README.md b/solution/0400-0499/0487.Max Consecutive Ones II/README.md index 85f3c157102cf..3e68e0c48f2a3 100644 --- a/solution/0400-0499/0487.Max Consecutive Ones II/README.md +++ b/solution/0400-0499/0487.Max Consecutive Ones II/README.md @@ -57,15 +57,15 @@ tags: -### 方法一:预处理 + 枚举 +### 方法一:滑动窗口 -定义 `left`, `right` 数组表示以第 $i$ 个元素结尾(开头),往前(往后)累计的最大连续 $1$ 的个数。 +我们可以遍历数组,用一个变量 $\textit{cnt}$ 记录当前窗口中 0 的个数,当 $\textit{cnt} > 1$ 时,我们将窗口的左边界右移一位。 -先遍历 `nums`,预处理出 `left` 和 `right`。 +遍历结束后,窗口的长度即为最大连续 1 的个数。 -然后枚举 `nums` 每个位置 $i$,统计以 $i$ 为分界点,左右两边最大连续 $1$ 的个数之和,取最大值即可。 +注意,在上述过程中,我们不需要循环将窗口的左边界右移,而是直接将左边界右移一位,这是因为,题目求的是最大连续 1 的个数,因此,窗口的长度只会增加,不会减少,所以我们不需要循环右移左边界。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 @@ -74,180 +74,13 @@ tags: ```python class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - ans = nums.count(1) - n = len(nums) - left = [0] * n - right = [0] * n - for i, v in enumerate(nums): - if v: - left[i] = 1 if i == 0 else left[i - 1] + 1 - for i in range(n - 1, -1, -1): - v = nums[i] - if v: - right[i] = 1 if i == n - 1 else right[i + 1] + 1 - ans = 0 - for i, v in enumerate(nums): - t = 0 - if i: - t += left[i - 1] - if i < n - 1: - t += right[i + 1] - ans = max(ans, t + 1) - return ans -``` - -#### Java - -```java -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int n = nums.length; - int[] left = new int[n]; - int[] right = new int[n]; - for (int i = 0; i < n; ++i) { - if (nums[i] == 1) { - left[i] = i == 0 ? 1 : left[i - 1] + 1; - } - } - for (int i = n - 1; i >= 0; --i) { - if (nums[i] == 1) { - right[i] = i == n - 1 ? 1 : right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - int t = 0; - if (i > 0) { - t += left[i - 1]; - } - if (i < n - 1) { - t += right[i + 1]; - } - ans = Math.max(ans, t + 1); - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int n = nums.size(); - vector left(n), right(n); - for (int i = 0; i < n; ++i) { - if (nums[i]) { - left[i] = i == 0 ? 1 : left[i - 1] + 1; - } - } - for (int i = n - 1; ~i; --i) { - if (nums[i]) { - right[i] = i == n - 1 ? 1 : right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - int t = 0; - if (i) { - t += left[i - 1]; - } - if (i < n - 1) { - t += right[i + 1]; - } - ans = max(ans, t + 1); - } - return ans; - } -}; -``` - -#### Go - -```go -func findMaxConsecutiveOnes(nums []int) int { - n := len(nums) - left := make([]int, n) - right := make([]int, n) - for i, v := range nums { - if v == 1 { - if i == 0 { - left[i] = 1 - } else { - left[i] = left[i-1] + 1 - } - } - } - for i := n - 1; i >= 0; i-- { - if nums[i] == 1 { - if i == n-1 { - right[i] = 1 - } else { - right[i] = right[i+1] + 1 - } - } - } - ans := 0 - for i := range nums { - t := 0 - if i > 0 { - t += left[i-1] - } - if i < n-1 { - t += right[i+1] - } - ans = max(ans, t+1) - } - return ans -} -``` - - - - - - - -### 方法二:滑动窗口 - -找出最大的窗口,使得窗口内的 $0$ 的个数不超过 $1$ 个。 - -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `nums` 的长度。 - -相似题目: - -- [1004. 最大连续 1 的个数 II 🔒I](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README.md) - -以下是滑动窗口的优化版本。 - -维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为求的是“最大”,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。 - -- l 是窗口左端点,负责移动起始位置 -- r 是窗口右端点,负责扩展窗口 -- k 是资源数,每次要替换 0,k 减 1,同时 r 向右移动 -- `r++` 每次都会执行,`l++` 只有资源 `k < 0` 时才触发,因此 `r - l` 的值只会单调递增(或保持不变) -- 移动左端点时,如果当前元素是 0,说明可以释放一个资源,k 加 1 - - - -#### Python3 - -```python -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - ans = 1 - cnt = j = 0 - for i, v in enumerate(nums): - if v == 0: - cnt += 1 - while cnt > 1: - if nums[j] == 0: - cnt -= 1 - j += 1 - ans = max(ans, i - j + 1) - return ans + l = cnt = 0 + for x in nums: + cnt += x ^ 1 + if cnt > 1: + cnt -= nums[l] ^ 1 + l += 1 + return len(nums) - l ``` #### Java @@ -255,20 +88,14 @@ class Solution: ```java class Solution { public int findMaxConsecutiveOnes(int[] nums) { - int j = 0, cnt = 0; - int ans = 1; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++cnt; + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - while (cnt > 1) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = Math.max(ans, i - j + 1); } - return ans; + return nums.length - l; } } ``` @@ -279,20 +106,14 @@ class Solution { class Solution { public: int findMaxConsecutiveOnes(vector& nums) { - int ans = 1; - int cnt = 0, j = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > 1) { - if (nums[j++] == 0) { - --cnt; - } + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - ans = max(ans, i - j + 1); } - return ans; + return nums.size() - l; } }; ``` @@ -301,114 +122,52 @@ public: ```go func findMaxConsecutiveOnes(nums []int) int { - ans := 1 - j, cnt := 0, 0 - for i, v := range nums { - if v == 0 { - cnt++ - } - for cnt > 1 { - if nums[j] == 0 { - cnt-- - } - j++ + l, cnt := 0, 0 + for _, x := range nums { + cnt += x ^ 1 + if cnt > 1 { + cnt -= nums[l] ^ 1 + l++ } - ans = max(ans, i-j+1) } - return ans + return len(nums) - l } ``` - - - - - - -### 方法三 - - - -#### Python3 - -```python -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - l = r = 0 - k = 1 - while r < len(nums): - if nums[r] == 0: - k -= 1 - if k < 0: - if nums[l] == 0: - k += 1 - l += 1 - r += 1 - return r - l -``` - -#### Java +#### TypeScript -```java -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int l = 0, r = 0; - int k = 1; - while (r < nums.length) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } +```ts +function findMaxConsecutiveOnes(nums: number[]): number { + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - return r - l; } + return nums.length - l; } ``` -#### C++ - -```cpp -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int l = 0, r = 0; - int k = 1; - while (r < nums.size()) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var findMaxConsecutiveOnes = function (nums) { + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - return r - l; } + return nums.length - l; }; ``` -#### Go - -```go -func findMaxConsecutiveOnes(nums []int) int { - l, r := 0, 0 - k := 1 - for ; r < len(nums); r++ { - if nums[r] == 0 { - k-- - } - if k < 0 { - if nums[l] == 0 { - k++ - } - l++ - } - } - return r - l -} -``` - diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md b/solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md index 7d66452586c03..b31ee8eaefa8b 100644 --- a/solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md +++ b/solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md @@ -60,152 +60,15 @@ The max number of consecutive ones is 4. -### Solution 1 +### Solution 1: Sliding Window - - -#### Python3 - -```python -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - ans = nums.count(1) - n = len(nums) - left = [0] * n - right = [0] * n - for i, v in enumerate(nums): - if v: - left[i] = 1 if i == 0 else left[i - 1] + 1 - for i in range(n - 1, -1, -1): - v = nums[i] - if v: - right[i] = 1 if i == n - 1 else right[i + 1] + 1 - ans = 0 - for i, v in enumerate(nums): - t = 0 - if i: - t += left[i - 1] - if i < n - 1: - t += right[i + 1] - ans = max(ans, t + 1) - return ans -``` - -#### Java - -```java -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int n = nums.length; - int[] left = new int[n]; - int[] right = new int[n]; - for (int i = 0; i < n; ++i) { - if (nums[i] == 1) { - left[i] = i == 0 ? 1 : left[i - 1] + 1; - } - } - for (int i = n - 1; i >= 0; --i) { - if (nums[i] == 1) { - right[i] = i == n - 1 ? 1 : right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - int t = 0; - if (i > 0) { - t += left[i - 1]; - } - if (i < n - 1) { - t += right[i + 1]; - } - ans = Math.max(ans, t + 1); - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int n = nums.size(); - vector left(n), right(n); - for (int i = 0; i < n; ++i) { - if (nums[i]) { - left[i] = i == 0 ? 1 : left[i - 1] + 1; - } - } - for (int i = n - 1; ~i; --i) { - if (nums[i]) { - right[i] = i == n - 1 ? 1 : right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - int t = 0; - if (i) { - t += left[i - 1]; - } - if (i < n - 1) { - t += right[i + 1]; - } - ans = max(ans, t + 1); - } - return ans; - } -}; -``` - -#### Go - -```go -func findMaxConsecutiveOnes(nums []int) int { - n := len(nums) - left := make([]int, n) - right := make([]int, n) - for i, v := range nums { - if v == 1 { - if i == 0 { - left[i] = 1 - } else { - left[i] = left[i-1] + 1 - } - } - } - for i := n - 1; i >= 0; i-- { - if nums[i] == 1 { - if i == n-1 { - right[i] = 1 - } else { - right[i] = right[i+1] + 1 - } - } - } - ans := 0 - for i := range nums { - t := 0 - if i > 0 { - t += left[i-1] - } - if i < n-1 { - t += right[i+1] - } - ans = max(ans, t+1) - } - return ans -} -``` +We can iterate through the array, using a variable $\textit{cnt}$ to record the current number of 0s in the window. When $\textit{cnt} > 1$, we move the left boundary of the window to the right by one position. - +After the iteration ends, the length of the window is the maximum number of consecutive 1s. - +Note that in the process above, we do not need to loop to move the left boundary of the window to the right. Instead, we directly move the left boundary to the right by one position. This is because the problem asks for the maximum number of consecutive 1s, so the length of the window will only increase, not decrease. Therefore, we do not need to loop to move the left boundary to the right. - - -### Solution 2 +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -214,17 +77,13 @@ func findMaxConsecutiveOnes(nums []int) int { ```python class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - ans = 1 - cnt = j = 0 - for i, v in enumerate(nums): - if v == 0: - cnt += 1 - while cnt > 1: - if nums[j] == 0: - cnt -= 1 - j += 1 - ans = max(ans, i - j + 1) - return ans + l = cnt = 0 + for x in nums: + cnt += x ^ 1 + if cnt > 1: + cnt -= nums[l] ^ 1 + l += 1 + return len(nums) - l ``` #### Java @@ -232,20 +91,14 @@ class Solution: ```java class Solution { public int findMaxConsecutiveOnes(int[] nums) { - int j = 0, cnt = 0; - int ans = 1; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++cnt; + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - while (cnt > 1) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = Math.max(ans, i - j + 1); } - return ans; + return nums.length - l; } } ``` @@ -256,20 +109,14 @@ class Solution { class Solution { public: int findMaxConsecutiveOnes(vector& nums) { - int ans = 1; - int cnt = 0, j = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > 1) { - if (nums[j++] == 0) { - --cnt; - } + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - ans = max(ans, i - j + 1); } - return ans; + return nums.size() - l; } }; ``` @@ -278,114 +125,52 @@ public: ```go func findMaxConsecutiveOnes(nums []int) int { - ans := 1 - j, cnt := 0, 0 - for i, v := range nums { - if v == 0 { - cnt++ - } - for cnt > 1 { - if nums[j] == 0 { - cnt-- - } - j++ + l, cnt := 0, 0 + for _, x := range nums { + cnt += x ^ 1 + if cnt > 1 { + cnt -= nums[l] ^ 1 + l++ } - ans = max(ans, i-j+1) } - return ans + return len(nums) - l } ``` - - - - - - -### Solution 3 - - - -#### Python3 - -```python -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - l = r = 0 - k = 1 - while r < len(nums): - if nums[r] == 0: - k -= 1 - if k < 0: - if nums[l] == 0: - k += 1 - l += 1 - r += 1 - return r - l -``` +#### TypeScript -#### Java - -```java -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int l = 0, r = 0; - int k = 1; - while (r < nums.length) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } +```ts +function findMaxConsecutiveOnes(nums: number[]): number { + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - return r - l; } + return nums.length - l; } ``` -#### C++ - -```cpp -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int l = 0, r = 0; - int k = 1; - while (r < nums.size()) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var findMaxConsecutiveOnes = function (nums) { + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; } - return r - l; } + return nums.length - l; }; ``` -#### Go - -```go -func findMaxConsecutiveOnes(nums []int) int { - l, r := 0, 0 - k := 1 - for ; r < len(nums); r++ { - if nums[r] == 0 { - k-- - } - if k < 0 { - if nums[l] == 0 { - k++ - } - l++ - } - } - return r - l -} -``` - diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.cpp b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.cpp index 350f8089a2c95..baee3d454b354 100644 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.cpp +++ b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.cpp @@ -1,29 +1,13 @@ -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int n = nums.size(); - vector left(n), right(n); - for (int i = 0; i < n; ++i) { - if (nums[i]) { - left[i] = i == 0 ? 1 : left[i - 1] + 1; - } - } - for (int i = n - 1; ~i; --i) { - if (nums[i]) { - right[i] = i == n - 1 ? 1 : right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - int t = 0; - if (i) { - t += left[i - 1]; - } - if (i < n - 1) { - t += right[i + 1]; - } - ans = max(ans, t + 1); - } - return ans; - } +class Solution { +public: + int findMaxConsecutiveOnes(vector& nums) { + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; + } + } + return nums.size() - l; + } }; \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.go b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.go index c0ef60889a6a5..224e994dd8b05 100644 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.go +++ b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.go @@ -1,35 +1,11 @@ -func findMaxConsecutiveOnes(nums []int) int { - n := len(nums) - left := make([]int, n) - right := make([]int, n) - for i, v := range nums { - if v == 1 { - if i == 0 { - left[i] = 1 - } else { - left[i] = left[i-1] + 1 - } - } - } - for i := n - 1; i >= 0; i-- { - if nums[i] == 1 { - if i == n-1 { - right[i] = 1 - } else { - right[i] = right[i+1] + 1 - } - } - } - ans := 0 - for i := range nums { - t := 0 - if i > 0 { - t += left[i-1] - } - if i < n-1 { - t += right[i+1] - } - ans = max(ans, t+1) - } - return ans +func findMaxConsecutiveOnes(nums []int) int { + l, cnt := 0, 0 + for _, x := range nums { + cnt += x ^ 1 + if cnt > 1 { + cnt -= nums[l] ^ 1 + l++ + } + } + return len(nums) - l } \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.java b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.java index aff3a28d350f1..519324bebd92c 100644 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.java +++ b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.java @@ -1,29 +1,12 @@ -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int n = nums.length; - int[] left = new int[n]; - int[] right = new int[n]; - for (int i = 0; i < n; ++i) { - if (nums[i] == 1) { - left[i] = i == 0 ? 1 : left[i - 1] + 1; - } - } - for (int i = n - 1; i >= 0; --i) { - if (nums[i] == 1) { - right[i] = i == n - 1 ? 1 : right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - int t = 0; - if (i > 0) { - t += left[i - 1]; - } - if (i < n - 1) { - t += right[i + 1]; - } - ans = Math.max(ans, t + 1); - } - return ans; - } +class Solution { + public int findMaxConsecutiveOnes(int[] nums) { + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; + } + } + return nums.length - l; + } } \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.js b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.js new file mode 100644 index 0000000000000..288f132c0de99 --- /dev/null +++ b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var findMaxConsecutiveOnes = function (nums) { + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; + } + } + return nums.length - l; +}; diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.py b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.py index c68b049c5c78f..5555f9a6344dd 100644 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.py +++ b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.py @@ -1,22 +1,9 @@ -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - ans = nums.count(1) - n = len(nums) - left = [0] * n - right = [0] * n - for i, v in enumerate(nums): - if v: - left[i] = 1 if i == 0 else left[i - 1] + 1 - for i in range(n - 1, -1, -1): - v = nums[i] - if v: - right[i] = 1 if i == n - 1 else right[i + 1] + 1 - ans = 0 - for i, v in enumerate(nums): - t = 0 - if i: - t += left[i - 1] - if i < n - 1: - t += right[i + 1] - ans = max(ans, t + 1) - return ans +class Solution: + def findMaxConsecutiveOnes(self, nums: List[int]) -> int: + l = cnt = 0 + for x in nums: + cnt += x ^ 1 + if cnt > 1: + cnt -= nums[l] ^ 1 + l += 1 + return len(nums) - l diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution.ts b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.ts new file mode 100644 index 0000000000000..55f17768d3f30 --- /dev/null +++ b/solution/0400-0499/0487.Max Consecutive Ones II/Solution.ts @@ -0,0 +1,10 @@ +function findMaxConsecutiveOnes(nums: number[]): number { + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > 1) { + cnt -= nums[l++] ^ 1; + } + } + return nums.length - l; +} diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.cpp b/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.cpp deleted file mode 100644 index 0e41eacc38733..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.cpp +++ /dev/null @@ -1,19 +0,0 @@ -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int ans = 1; - int cnt = 0, j = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > 1) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = max(ans, i - j + 1); - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.go b/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.go deleted file mode 100644 index 137e6bcb7bf48..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.go +++ /dev/null @@ -1,17 +0,0 @@ -func findMaxConsecutiveOnes(nums []int) int { - ans := 1 - j, cnt := 0, 0 - for i, v := range nums { - if v == 0 { - cnt++ - } - for cnt > 1 { - if nums[j] == 0 { - cnt-- - } - j++ - } - ans = max(ans, i-j+1) - } - return ans -} \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.java b/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.java deleted file mode 100644 index 832bb7c7058df..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.java +++ /dev/null @@ -1,18 +0,0 @@ -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int j = 0, cnt = 0; - int ans = 1; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > 1) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = Math.max(ans, i - j + 1); - } - return ans; - } -} \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.py b/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.py deleted file mode 100644 index a5f7c004d8524..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution2.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - ans = 1 - cnt = j = 0 - for i, v in enumerate(nums): - if v == 0: - cnt += 1 - while cnt > 1: - if nums[j] == 0: - cnt -= 1 - j += 1 - ans = max(ans, i - j + 1) - return ans diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.cpp b/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.cpp deleted file mode 100644 index 178e9b80178e1..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.cpp +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int l = 0, r = 0; - int k = 1; - while (r < nums.size()) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } - } - return r - l; - } -}; \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.go b/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.go deleted file mode 100644 index b0470bf69e722..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.go +++ /dev/null @@ -1,16 +0,0 @@ -func findMaxConsecutiveOnes(nums []int) int { - l, r := 0, 0 - k := 1 - for ; r < len(nums); r++ { - if nums[r] == 0 { - k-- - } - if k < 0 { - if nums[l] == 0 { - k++ - } - l++ - } - } - return r - l -} \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.java b/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.java deleted file mode 100644 index 54b4fd3b93cff..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.java +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int l = 0, r = 0; - int k = 1; - while (r < nums.length) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } - } - return r - l; - } -} \ No newline at end of file diff --git a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.py b/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.py deleted file mode 100644 index b7054bfad1627..0000000000000 --- a/solution/0400-0499/0487.Max Consecutive Ones II/Solution3.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution: - def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - l = r = 0 - k = 1 - while r < len(nums): - if nums[r] == 0: - k -= 1 - if k < 0: - if nums[l] == 0: - k += 1 - l += 1 - r += 1 - return r - l diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/README.md b/solution/1000-1099/1004.Max Consecutive Ones III/README.md index 4134f32a13636..d29d0e22e9a58 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/README.md +++ b/solution/1000-1099/1004.Max Consecutive Ones III/README.md @@ -59,7 +59,11 @@ tags: ### 方法一:滑动窗口 -我们定义一个滑动窗口,窗口内的 $0$ 的个数不超过 $k$,窗口的右边界不断向右移动,当窗口内的 $0$ 的个数超过 $k$ 时,窗口的左边界向右移动,直到窗口内的 $0$ 的个数不超过 $k$ 为止。 +我们可以遍历数组,用一个变量 $\textit{cnt}$ 记录当前窗口中 0 的个数,当 $\textit{cnt} > k$ 时,我们将窗口的左边界右移一位。 + +遍历结束后,窗口的长度即为最大连续 1 的个数。 + +注意,在上述过程中,我们不需要循环将窗口的左边界右移,而是直接将左边界右移一位,这是因为,题目求的是最大连续 1 的个数,因此,窗口的长度只会增加,不会减少,所以我们不需要循环右移左边界。 时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 @@ -75,142 +79,13 @@ tags: ```python class Solution: def longestOnes(self, nums: List[int], k: int) -> int: - ans = 0 - cnt = j = 0 - for i, v in enumerate(nums): - if v == 0: - cnt += 1 - while cnt > k: - if nums[j] == 0: - cnt -= 1 - j += 1 - ans = max(ans, i - j + 1) - return ans -``` - -#### Java - -```java -class Solution { - public int longestOnes(int[] nums, int k) { - int j = 0, cnt = 0; - int ans = 0; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > k) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = Math.max(ans, i - j + 1); - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int longestOnes(vector& nums, int k) { - int ans = 0; - int cnt = 0, j = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > k) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = max(ans, i - j + 1); - } - return ans; - } -}; -``` - -#### Go - -```go -func longestOnes(nums []int, k int) int { - ans := 0 - j, cnt := 0, 0 - for i, v := range nums { - if v == 0 { - cnt++ - } - for cnt > k { - if nums[j] == 0 { - cnt-- - } - j++ - } - ans = max(ans, i-j+1) - } - return ans -} -``` - -#### TypeScript - -```ts -function longestOnes(nums: number[], k: number): number { - const n = nums.length; - let [ans, cnt, j] = [0, 0, 0]; - for (let i = 0; i < n; ++i) { - cnt += nums[i] ^ 1; - while (cnt > k) { - cnt -= nums[j++] ^ 1; - } - ans = Math.max(ans, i - j + 1); - } - return ans; -} -``` - - - - - - - -### 方法二:滑动窗口(优化) - -以下是滑动窗口的优化版本。 - -维护一个单调变长的窗口。这种窗口经常出现在寻求“最大窗口”的问题中:因为求的是“最大”,所以我们没有必要缩短窗口,于是代码就少了缩短窗口的部分;从另一个角度讲,本题里的 K 是资源数,一旦透支,窗口就不能再增长了。 - -- l 是窗口左端点,负责移动起始位置 -- r 是窗口右端点,负责扩展窗口 -- k 是资源数,每次要替换 0,k 减 1,同时 r 向右移动 -- `r++` 每次都会执行,`l++` 只有资源 `k < 0` 时才触发,因此 `r - l` 的值只会单调递增(或保持不变) -- 移动左端点时,如果当前元素是 0,说明可以释放一个资源,k 加 1 - -时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 - - - -#### Python3 - -```python -class Solution: - def longestOnes(self, nums: List[int], k: int) -> int: - l = r = -1 - while r < len(nums) - 1: - r += 1 - if nums[r] == 0: - k -= 1 - if k < 0: + l = cnt = 0 + for x in nums: + cnt += x ^ 1 + if cnt > k: + cnt -= nums[l] ^ 1 l += 1 - if nums[l] == 0: - k += 1 - return r - l + return len(nums) - l ``` #### Java @@ -218,16 +93,14 @@ class Solution: ```java class Solution { public int longestOnes(int[] nums, int k) { - int l = 0, r = 0; - while (r < nums.length) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; } } - return r - l; + return nums.length - l; } } ``` @@ -238,12 +111,14 @@ class Solution { class Solution { public: int longestOnes(vector& nums, int k) { - int l = 0, r = 0; - while (r < nums.size()) { - if (nums[r++] == 0) --k; - if (k < 0 && nums[l++] == 0) ++k; + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; + } } - return r - l; + return nums.size() - l; } }; ``` @@ -252,20 +127,15 @@ public: ```go func longestOnes(nums []int, k int) int { - l, r := -1, -1 - for r < len(nums)-1 { - r++ - if nums[r] == 0 { - k-- - } - if k < 0 { + l, cnt := 0, 0 + for _, x := range nums { + cnt += x ^ 1 + if cnt > k { + cnt -= nums[l] ^ 1 l++ - if nums[l] == 0 { - k++ - } } } - return r - l + return len(nums) - l } ``` @@ -273,40 +143,14 @@ func longestOnes(nums []int, k int) int { ```ts function longestOnes(nums: number[], k: number): number { - const n = nums.length; - let l = 0; - for (const num of nums) { - if (num === 0) { - k--; - } - if (k < 0 && nums[l++] === 0) { - k++; - } - } - return n - l; -} -``` - -#### Rust - -```rust -impl Solution { - pub fn longest_ones(nums: Vec, mut k: i32) -> i32 { - let n = nums.len(); - let mut l = 0; - for num in nums.iter() { - if num == &0 { - k -= 1; - } - if k < 0 { - if nums[l] == 0 { - k += 1; - } - l += 1; - } + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; } - (n - l) as i32 } + return nums.length - l; } ``` diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/README_EN.md b/solution/1000-1099/1004.Max Consecutive Ones III/README_EN.md index 1e7fb9bdd3446..ea4657f055960 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/README_EN.md +++ b/solution/1000-1099/1004.Max Consecutive Ones III/README_EN.md @@ -58,7 +58,11 @@ Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. ### Solution 1: Sliding Window -We define a sliding window, the number of $0$s in the window does not exceed $k$. The right boundary of the window keeps moving to the right. When the number of $0$s in the window exceeds $k$, the left boundary of the window moves to the right until the number of $0$s in the window does not exceed $k$. +We can iterate through the array, using a variable $\textit{cnt}$ to record the current number of 0s in the window. When $\textit{cnt} > k$, we move the left boundary of the window to the right by one position. + +After the iteration ends, the length of the window is the maximum number of consecutive 1s. + +Note that in the process above, we do not need to loop to move the left boundary of the window to the right. Instead, we directly move the left boundary to the right by one position. This is because the problem asks for the maximum number of consecutive 1s, so the length of the window will only increase, not decrease. Therefore, we do not need to loop to move the left boundary to the right. The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -74,142 +78,13 @@ Similar problems: ```python class Solution: def longestOnes(self, nums: List[int], k: int) -> int: - ans = 0 - cnt = j = 0 - for i, v in enumerate(nums): - if v == 0: - cnt += 1 - while cnt > k: - if nums[j] == 0: - cnt -= 1 - j += 1 - ans = max(ans, i - j + 1) - return ans -``` - -#### Java - -```java -class Solution { - public int longestOnes(int[] nums, int k) { - int j = 0, cnt = 0; - int ans = 0; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > k) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = Math.max(ans, i - j + 1); - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int longestOnes(vector& nums, int k) { - int ans = 0; - int cnt = 0, j = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > k) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = max(ans, i - j + 1); - } - return ans; - } -}; -``` - -#### Go - -```go -func longestOnes(nums []int, k int) int { - ans := 0 - j, cnt := 0, 0 - for i, v := range nums { - if v == 0 { - cnt++ - } - for cnt > k { - if nums[j] == 0 { - cnt-- - } - j++ - } - ans = max(ans, i-j+1) - } - return ans -} -``` - -#### TypeScript - -```ts -function longestOnes(nums: number[], k: number): number { - const n = nums.length; - let [ans, cnt, j] = [0, 0, 0]; - for (let i = 0; i < n; ++i) { - cnt += nums[i] ^ 1; - while (cnt > k) { - cnt -= nums[j++] ^ 1; - } - ans = Math.max(ans, i - j + 1); - } - return ans; -} -``` - - - - - - - -### Solution 2: Sliding Window (Optimized) - -Below is the optimized version of the sliding window. - -Maintain a monotonically variable-length window. This kind of window often appears in problems seeking the "maximum window": because we are seeking the "maximum", there is no need to shorten the window, so the code lacks the part to shorten the window; from another perspective, the K in this problem is the number of resources, once overdrawn, the window can no longer grow. - -- `l` is the left endpoint of the window, responsible for moving the starting position -- `r` is the right endpoint of the window, responsible for expanding the window -- `k` is the number of resources, each time a 0 needs to be replaced, `k` decreases by 1, and `r` moves to the right at the same time -- `r++` will be executed every time, `l++` is only triggered when the resource `k < 0`, therefore the value of `r - l` will only increase monotonically (or remain unchanged) -- When moving the left endpoint, if the current element is 0, it means that a resource can be released, `k` increases by 1 - -The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. - - - -#### Python3 - -```python -class Solution: - def longestOnes(self, nums: List[int], k: int) -> int: - l = r = -1 - while r < len(nums) - 1: - r += 1 - if nums[r] == 0: - k -= 1 - if k < 0: + l = cnt = 0 + for x in nums: + cnt += x ^ 1 + if cnt > k: + cnt -= nums[l] ^ 1 l += 1 - if nums[l] == 0: - k += 1 - return r - l + return len(nums) - l ``` #### Java @@ -217,16 +92,14 @@ class Solution: ```java class Solution { public int longestOnes(int[] nums, int k) { - int l = 0, r = 0; - while (r < nums.length) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; } } - return r - l; + return nums.length - l; } } ``` @@ -237,12 +110,14 @@ class Solution { class Solution { public: int longestOnes(vector& nums, int k) { - int l = 0, r = 0; - while (r < nums.size()) { - if (nums[r++] == 0) --k; - if (k < 0 && nums[l++] == 0) ++k; + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; + } } - return r - l; + return nums.size() - l; } }; ``` @@ -251,20 +126,15 @@ public: ```go func longestOnes(nums []int, k int) int { - l, r := -1, -1 - for r < len(nums)-1 { - r++ - if nums[r] == 0 { - k-- - } - if k < 0 { + l, cnt := 0, 0 + for _, x := range nums { + cnt += x ^ 1 + if cnt > k { + cnt -= nums[l] ^ 1 l++ - if nums[l] == 0 { - k++ - } } } - return r - l + return len(nums) - l } ``` @@ -272,40 +142,14 @@ func longestOnes(nums []int, k int) int { ```ts function longestOnes(nums: number[], k: number): number { - const n = nums.length; - let l = 0; - for (const num of nums) { - if (num === 0) { - k--; - } - if (k < 0 && nums[l++] === 0) { - k++; - } - } - return n - l; -} -``` - -#### Rust - -```rust -impl Solution { - pub fn longest_ones(nums: Vec, mut k: i32) -> i32 { - let n = nums.len(); - let mut l = 0; - for num in nums.iter() { - if num == &0 { - k -= 1; - } - if k < 0 { - if nums[l] == 0 { - k += 1; - } - l += 1; - } + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; } - (n - l) as i32 } + return nums.length - l; } ``` diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp index 923c677683956..e796140d8256e 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp +++ b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp @@ -1,19 +1,13 @@ -class Solution { -public: - int longestOnes(vector& nums, int k) { - int ans = 0; - int cnt = 0, j = 0; - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > k) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = max(ans, i - j + 1); - } - return ans; - } +class Solution { +public: + int longestOnes(vector& nums, int k) { + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; + } + } + return nums.size() - l; + } }; \ No newline at end of file diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.go b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.go index 3419554a0d09a..84ea8133b1401 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.go +++ b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.go @@ -1,17 +1,11 @@ -func longestOnes(nums []int, k int) int { - ans := 0 - j, cnt := 0, 0 - for i, v := range nums { - if v == 0 { - cnt++ - } - for cnt > k { - if nums[j] == 0 { - cnt-- - } - j++ - } - ans = max(ans, i-j+1) - } - return ans +func longestOnes(nums []int, k int) int { + l, cnt := 0, 0 + for _, x := range nums { + cnt += x ^ 1 + if cnt > k { + cnt -= nums[l] ^ 1 + l++ + } + } + return len(nums) - l } \ No newline at end of file diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.java b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.java index e3285860f7b76..2168da616cc4e 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.java +++ b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.java @@ -1,18 +1,12 @@ -class Solution { - public int longestOnes(int[] nums, int k) { - int j = 0, cnt = 0; - int ans = 0; - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++cnt; - } - while (cnt > k) { - if (nums[j++] == 0) { - --cnt; - } - } - ans = Math.max(ans, i - j + 1); - } - return ans; - } +class Solution { + public int longestOnes(int[] nums, int k) { + int l = 0, cnt = 0; + for (int x : nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; + } + } + return nums.length - l; + } } \ No newline at end of file diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.py b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.py index 868a067e7a2ca..4159cd16d5244 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.py +++ b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.py @@ -1,13 +1,9 @@ -class Solution: - def longestOnes(self, nums: List[int], k: int) -> int: - ans = 0 - cnt = j = 0 - for i, v in enumerate(nums): - if v == 0: - cnt += 1 - while cnt > k: - if nums[j] == 0: - cnt -= 1 - j += 1 - ans = max(ans, i - j + 1) - return ans +class Solution: + def longestOnes(self, nums: List[int], k: int) -> int: + l = cnt = 0 + for x in nums: + cnt += x ^ 1 + if cnt > k: + cnt -= nums[l] ^ 1 + l += 1 + return len(nums) - l diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.ts b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.ts index 54034d01d65d4..156534a2267b1 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.ts +++ b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.ts @@ -1,12 +1,10 @@ function longestOnes(nums: number[], k: number): number { - const n = nums.length; - let [ans, cnt, j] = [0, 0, 0]; - for (let i = 0; i < n; ++i) { - cnt += nums[i] ^ 1; - while (cnt > k) { - cnt -= nums[j++] ^ 1; + let [l, cnt] = [0, 0]; + for (const x of nums) { + cnt += x ^ 1; + if (cnt > k) { + cnt -= nums[l++] ^ 1; } - ans = Math.max(ans, i - j + 1); } - return ans; + return nums.length - l; } diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.cpp b/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.cpp deleted file mode 100644 index cdf78071b9df8..0000000000000 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.cpp +++ /dev/null @@ -1,11 +0,0 @@ -class Solution { -public: - int longestOnes(vector& nums, int k) { - int l = 0, r = 0; - while (r < nums.size()) { - if (nums[r++] == 0) --k; - if (k < 0 && nums[l++] == 0) ++k; - } - return r - l; - } -}; \ No newline at end of file diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.go b/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.go deleted file mode 100644 index 64dc822dd8900..0000000000000 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.go +++ /dev/null @@ -1,16 +0,0 @@ -func longestOnes(nums []int, k int) int { - l, r := -1, -1 - for r < len(nums)-1 { - r++ - if nums[r] == 0 { - k-- - } - if k < 0 { - l++ - if nums[l] == 0 { - k++ - } - } - } - return r - l -} \ No newline at end of file diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.java b/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.java deleted file mode 100644 index 5771abd5d3330..0000000000000 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.java +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { - public int longestOnes(int[] nums, int k) { - int l = 0, r = 0; - while (r < nums.length) { - if (nums[r++] == 0) { - --k; - } - if (k < 0 && nums[l++] == 0) { - ++k; - } - } - return r - l; - } -} \ No newline at end of file diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.py b/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.py deleted file mode 100644 index 77ec15b9cd4ba..0000000000000 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution: - def longestOnes(self, nums: List[int], k: int) -> int: - l = r = -1 - while r < len(nums) - 1: - r += 1 - if nums[r] == 0: - k -= 1 - if k < 0: - l += 1 - if nums[l] == 0: - k += 1 - return r - l diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.rs b/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.rs deleted file mode 100644 index d8231150ae302..0000000000000 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.rs +++ /dev/null @@ -1,18 +0,0 @@ -impl Solution { - pub fn longest_ones(nums: Vec, mut k: i32) -> i32 { - let n = nums.len(); - let mut l = 0; - for num in nums.iter() { - if num == &0 { - k -= 1; - } - if k < 0 { - if nums[l] == 0 { - k += 1; - } - l += 1; - } - } - (n - l) as i32 - } -} diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.ts b/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.ts deleted file mode 100644 index 873c0ae6fe91d..0000000000000 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution2.ts +++ /dev/null @@ -1,13 +0,0 @@ -function longestOnes(nums: number[], k: number): number { - const n = nums.length; - let l = 0; - for (const num of nums) { - if (num === 0) { - k--; - } - if (k < 0 && nums[l++] === 0) { - k++; - } - } - return n - l; -} diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/README.md b/solution/2000-2099/2024.Maximize the Confusion of an Exam/README.md index 657d15a108179..503ee1c1d44a4 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/README.md +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/README.md @@ -79,14 +79,19 @@ tags: -### 方法一:双指针 +### 方法一:滑动窗口 -我们设计一个函数 $f(c)$,表示最多替换 $k$ 个字符 $c$ 的情况下,最长的连续字符的长度,其中 $c$ 可以是 'T' 或 'F'。答案就是 $\max(f('T'), f('F'))$。 +我们设计一个函数 $\textit{f}(c)$,表示最多替换 $k$ 个字符 $c$ 的情况下,最长的连续字符的长度,其中 $c$ 可以是 'T' 或 'F'。答案就是 $\max(\textit{f}('T'), \textit{f}('F'))$。 -我们使用双指针维护一个区间 $[j, i]$,使得区间内的字符 $c$ 的数量不超过 $k$。当区间内的字符 $c$ 的数量超过 $k$ 时,我们移动左指针 $j$,直到区间内的字符 $c$ 的数量不超过 $k$,然后更新答案 $ans = \max(ans, i - j + 1)$。 +我们遍历字符串 $\textit{answerKey}$,用一个变量 $\textit{cnt}$ 记录当前窗口内字符 $c$ 的个数,当 $\textit{cnt} > k$ 时,我们将窗口的左指针右移一位。遍历结束后,窗口的长度即为最大连续字符的长度。 时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 +相似题目: + +- [487. 最大连续1的个数 II](https://github.com/doocs/leetcode/blob/main/solution/0400-0499/0487.Max%20Consecutive%20Ones%20II/README.md) +- [1004. 最大连续1的个数 III](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README.md) + #### Python3 @@ -95,15 +100,13 @@ tags: class Solution: def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int: def f(c: str) -> int: - cnt = j = 0 - ans = 0 - for i, ch in enumerate(answerKey): + cnt = l = 0 + for ch in answerKey: cnt += ch == c - while cnt > k: - cnt -= answerKey[j] == c - j += 1 - ans = max(ans, i - j + 1) - return ans + if cnt > k: + cnt -= answerKey[l] == c + l += 1 + return len(answerKey) - l return max(f("T"), f("F")) ``` @@ -122,16 +125,14 @@ class Solution { } private int f(char c) { - int cnt = 0, ans = 0; - for (int i = 0, j = 0; i < s.length; ++i) { - cnt += s[i] == c ? 1 : 0; - while (cnt > k) { - cnt -= s[j] == c ? 1 : 0; - ++j; + int l = 0, cnt = 0; + for (char ch : s) { + cnt += ch == c ? 1 : 0; + if (cnt > k) { + cnt -= s[l++] == c ? 1 : 0; } - ans = Math.max(ans, i - j + 1); } - return ans; + return s.length - l; } } ``` @@ -142,16 +143,16 @@ class Solution { class Solution { public: int maxConsecutiveAnswers(string answerKey, int k) { + int n = answerKey.size(); auto f = [&](char c) { - int ans = 0, cnt = 0; - for (int i = 0, j = 0; i < answerKey.size(); ++i) { - cnt += answerKey[i] == c; - while (cnt > k) { - cnt -= answerKey[j++] == c; + int l = 0, cnt = 0; + for (char& ch : answerKey) { + cnt += ch == c; + if (cnt > k) { + cnt -= answerKey[l++] == c; } - ans = max(ans, i - j + 1); } - return ans; + return n - l; }; return max(f('T'), f('F')); } @@ -163,20 +164,19 @@ public: ```go func maxConsecutiveAnswers(answerKey string, k int) int { f := func(c byte) int { - var ans, cnt, j int - for i := range answerKey { - if answerKey[i] == c { + l, cnt := 0, 0 + for _, ch := range answerKey { + if byte(ch) == c { cnt++ } - for cnt > k { - if answerKey[j] == c { + if cnt > k { + if answerKey[l] == c { cnt-- } - j++ + l++ } - ans = max(ans, i-j+1) } - return ans + return len(answerKey) - l } return max(f('T'), f('F')) } @@ -188,15 +188,14 @@ func maxConsecutiveAnswers(answerKey string, k int) int { function maxConsecutiveAnswers(answerKey: string, k: number): number { const n = answerKey.length; const f = (c: string): number => { - let [ans, cnt, j] = [0, 0, 0]; - for (let i = 0; i < n; ++i) { - cnt += answerKey[i] === c ? 0 : 1; - while (cnt > k) { - cnt -= answerKey[j++] === c ? 0 : 1; + let [l, cnt] = [0, 0]; + for (const ch of answerKey) { + cnt += ch === c ? 1 : 0; + if (cnt > k) { + cnt -= answerKey[l++] === c ? 1 : 0; } - ans = Math.max(ans, i - j + 1); } - return ans; + return n - l; }; return Math.max(f('T'), f('F')); } @@ -207,22 +206,24 @@ function maxConsecutiveAnswers(answerKey: string, k: number): number { ```rust impl Solution { pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { + let n = answer_key.len(); + let k = k as usize; let s: Vec = answer_key.chars().collect(); - let f = |c: char| -> i32 { + + let f = |c: char| -> usize { + let mut l = 0; let mut cnt = 0; - let mut j = 0; - let mut ans = 0; - for i in 0..s.len() { - cnt += if s[i] == c { 1 } else { 0 }; - while cnt > k { - cnt -= if s[j] == c { 1 } else { 0 }; - j += 1; + for &ch in &s { + cnt += if ch == c { 1 } else { 0 }; + if cnt > k { + cnt -= if s[l] == c { 1 } else { 0 }; + l += 1; } - ans = ans.max((i - j + 1) as i32); } - ans + n - l }; - f('T').max(f('F')) + + std::cmp::max(f('T'), f('F')) as i32 } } ``` diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/README_EN.md b/solution/2000-2099/2024.Maximize the Confusion of an Exam/README_EN.md index 2c68d235a6ef7..a6cae143241e9 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/README_EN.md +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/README_EN.md @@ -77,13 +77,18 @@ In both cases, there are five consecutive 'T's. -### Solution 1: Two Pointers +### Solution 1: Sliding Window -We design a function $f(c)$, which represents the longest length of consecutive characters under the condition that at most $k$ characters $c$ are replaced, where $c$ can be 'T' or 'F'. The answer is $\max(f('T'), f('F'))$. +We design a function $\textit{f}(c)$, which represents the longest length of consecutive characters under the condition that at most $k$ characters $c$ can be replaced, where $c$ can be 'T' or 'F'. The answer is $\max(\textit{f}('T'), \textit{f}('F'))$. -We use two pointers to maintain a range $[j, i]$ such that the number of characters $c$ in the range does not exceed $k$. When the number of characters $c$ in the range exceeds $k$, we move the left pointer $j$ until the number of characters $c$ in the range does not exceed $k$, then update the answer $ans = \max(ans, i - j + 1)$. +We iterate through the string $\textit{answerKey}$, using a variable $\textit{cnt}$ to record the number of characters $c$ within the current window. When $\textit{cnt} > k$, we move the left pointer of the window one position to the right. After the iteration ends, the length of the window is the maximum length of consecutive characters. -The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$. +Time complexity is $O(n)$, where $n$ is the length of the string. Space complexity is $O(1)$. + +Similar problems: + +- [487. Max Consecutive Ones II](https://github.com/doocs/leetcode/blob/main/solution/0400-0499/0487.Max%20Consecutive%20Ones%20II/README_EN.md) +- [1004. Max Consecutive Ones III](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README_EN.md) @@ -93,15 +98,13 @@ The time complexity is $O(n)$, where $n$ is the length of the string. The space class Solution: def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int: def f(c: str) -> int: - cnt = j = 0 - ans = 0 - for i, ch in enumerate(answerKey): + cnt = l = 0 + for ch in answerKey: cnt += ch == c - while cnt > k: - cnt -= answerKey[j] == c - j += 1 - ans = max(ans, i - j + 1) - return ans + if cnt > k: + cnt -= answerKey[l] == c + l += 1 + return len(answerKey) - l return max(f("T"), f("F")) ``` @@ -120,16 +123,14 @@ class Solution { } private int f(char c) { - int cnt = 0, ans = 0; - for (int i = 0, j = 0; i < s.length; ++i) { - cnt += s[i] == c ? 1 : 0; - while (cnt > k) { - cnt -= s[j] == c ? 1 : 0; - ++j; + int l = 0, cnt = 0; + for (char ch : s) { + cnt += ch == c ? 1 : 0; + if (cnt > k) { + cnt -= s[l++] == c ? 1 : 0; } - ans = Math.max(ans, i - j + 1); } - return ans; + return s.length - l; } } ``` @@ -140,16 +141,16 @@ class Solution { class Solution { public: int maxConsecutiveAnswers(string answerKey, int k) { + int n = answerKey.size(); auto f = [&](char c) { - int ans = 0, cnt = 0; - for (int i = 0, j = 0; i < answerKey.size(); ++i) { - cnt += answerKey[i] == c; - while (cnt > k) { - cnt -= answerKey[j++] == c; + int l = 0, cnt = 0; + for (char& ch : answerKey) { + cnt += ch == c; + if (cnt > k) { + cnt -= answerKey[l++] == c; } - ans = max(ans, i - j + 1); } - return ans; + return n - l; }; return max(f('T'), f('F')); } @@ -161,20 +162,19 @@ public: ```go func maxConsecutiveAnswers(answerKey string, k int) int { f := func(c byte) int { - var ans, cnt, j int - for i := range answerKey { - if answerKey[i] == c { + l, cnt := 0, 0 + for _, ch := range answerKey { + if byte(ch) == c { cnt++ } - for cnt > k { - if answerKey[j] == c { + if cnt > k { + if answerKey[l] == c { cnt-- } - j++ + l++ } - ans = max(ans, i-j+1) } - return ans + return len(answerKey) - l } return max(f('T'), f('F')) } @@ -186,15 +186,14 @@ func maxConsecutiveAnswers(answerKey string, k int) int { function maxConsecutiveAnswers(answerKey: string, k: number): number { const n = answerKey.length; const f = (c: string): number => { - let [ans, cnt, j] = [0, 0, 0]; - for (let i = 0; i < n; ++i) { - cnt += answerKey[i] === c ? 0 : 1; - while (cnt > k) { - cnt -= answerKey[j++] === c ? 0 : 1; + let [l, cnt] = [0, 0]; + for (const ch of answerKey) { + cnt += ch === c ? 1 : 0; + if (cnt > k) { + cnt -= answerKey[l++] === c ? 1 : 0; } - ans = Math.max(ans, i - j + 1); } - return ans; + return n - l; }; return Math.max(f('T'), f('F')); } @@ -205,22 +204,24 @@ function maxConsecutiveAnswers(answerKey: string, k: number): number { ```rust impl Solution { pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { + let n = answer_key.len(); + let k = k as usize; let s: Vec = answer_key.chars().collect(); - let f = |c: char| -> i32 { + + let f = |c: char| -> usize { + let mut l = 0; let mut cnt = 0; - let mut j = 0; - let mut ans = 0; - for i in 0..s.len() { - cnt += if s[i] == c { 1 } else { 0 }; - while cnt > k { - cnt -= if s[j] == c { 1 } else { 0 }; - j += 1; + for &ch in &s { + cnt += if ch == c { 1 } else { 0 }; + if cnt > k { + cnt -= if s[l] == c { 1 } else { 0 }; + l += 1; } - ans = ans.max((i - j + 1) as i32); } - ans + n - l }; - f('T').max(f('F')) + + std::cmp::max(f('T'), f('F')) as i32 } } ``` diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp index 6609fa2839773..5a3f90020b886 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp @@ -1,16 +1,16 @@ class Solution { public: int maxConsecutiveAnswers(string answerKey, int k) { + int n = answerKey.size(); auto f = [&](char c) { - int ans = 0, cnt = 0; - for (int i = 0, j = 0; i < answerKey.size(); ++i) { - cnt += answerKey[i] == c; - while (cnt > k) { - cnt -= answerKey[j++] == c; + int l = 0, cnt = 0; + for (char& ch : answerKey) { + cnt += ch == c; + if (cnt > k) { + cnt -= answerKey[l++] == c; } - ans = max(ans, i - j + 1); } - return ans; + return n - l; }; return max(f('T'), f('F')); } diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.go b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.go index 8dfe6f9d3eff2..6e4428ccfdd22 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.go +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.go @@ -1,19 +1,18 @@ func maxConsecutiveAnswers(answerKey string, k int) int { f := func(c byte) int { - var ans, cnt, j int - for i := range answerKey { - if answerKey[i] == c { + l, cnt := 0, 0 + for _, ch := range answerKey { + if byte(ch) == c { cnt++ } - for cnt > k { - if answerKey[j] == c { + if cnt > k { + if answerKey[l] == c { cnt-- } - j++ + l++ } - ans = max(ans, i-j+1) } - return ans + return len(answerKey) - l } return max(f('T'), f('F')) } \ No newline at end of file diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.java b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.java index 92c3c43b79828..27029b4fbad81 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.java +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.java @@ -9,15 +9,13 @@ public int maxConsecutiveAnswers(String answerKey, int k) { } private int f(char c) { - int cnt = 0, ans = 0; - for (int i = 0, j = 0; i < s.length; ++i) { - cnt += s[i] == c ? 1 : 0; - while (cnt > k) { - cnt -= s[j] == c ? 1 : 0; - ++j; + int l = 0, cnt = 0; + for (char ch : s) { + cnt += ch == c ? 1 : 0; + if (cnt > k) { + cnt -= s[l++] == c ? 1 : 0; } - ans = Math.max(ans, i - j + 1); } - return ans; + return s.length - l; } } \ No newline at end of file diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.py b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.py index 48197f08fbd37..14bb9e0dc85b0 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.py +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.py @@ -1,14 +1,12 @@ class Solution: def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int: def f(c: str) -> int: - cnt = j = 0 - ans = 0 - for i, ch in enumerate(answerKey): + cnt = l = 0 + for ch in answerKey: cnt += ch == c - while cnt > k: - cnt -= answerKey[j] == c - j += 1 - ans = max(ans, i - j + 1) - return ans + if cnt > k: + cnt -= answerKey[l] == c + l += 1 + return len(answerKey) - l return max(f("T"), f("F")) diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.rs b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.rs index a8ec17950b136..1e7ec770e9dd0 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.rs +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.rs @@ -1,20 +1,22 @@ impl Solution { pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { + let n = answer_key.len(); + let k = k as usize; let s: Vec = answer_key.chars().collect(); - let f = |c: char| -> i32 { + + let f = |c: char| -> usize { + let mut l = 0; let mut cnt = 0; - let mut j = 0; - let mut ans = 0; - for i in 0..s.len() { - cnt += if s[i] == c { 1 } else { 0 }; - while cnt > k { - cnt -= if s[j] == c { 1 } else { 0 }; - j += 1; + for &ch in &s { + cnt += if ch == c { 1 } else { 0 }; + if cnt > k { + cnt -= if s[l] == c { 1 } else { 0 }; + l += 1; } - ans = ans.max((i - j + 1) as i32); } - ans + n - l }; - f('T').max(f('F')) + + std::cmp::max(f('T'), f('F')) as i32 } } diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.ts b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.ts index bcfe8aa1351f1..7f0d28018d0d1 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.ts +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.ts @@ -1,15 +1,14 @@ function maxConsecutiveAnswers(answerKey: string, k: number): number { const n = answerKey.length; const f = (c: string): number => { - let [ans, cnt, j] = [0, 0, 0]; - for (let i = 0; i < n; ++i) { - cnt += answerKey[i] === c ? 0 : 1; - while (cnt > k) { - cnt -= answerKey[j++] === c ? 0 : 1; + let [l, cnt] = [0, 0]; + for (const ch of answerKey) { + cnt += ch === c ? 1 : 0; + if (cnt > k) { + cnt -= answerKey[l++] === c ? 1 : 0; } - ans = Math.max(ans, i - j + 1); } - return ans; + return n - l; }; return Math.max(f('T'), f('F')); }