From fbe4e68d6e923af4dad250346b29aaac9a27c316 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 5 Nov 2024 09:21:29 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3261 No.3261.Count Substrings That Satisfy K-Constraint II --- .../README.md" | 14 +- .../README.md | 6 +- .../README_EN.md | 4 +- .../Solution.ts | 4 +- .../README.md | 141 +++++++++++++++++- .../README_EN.md | 141 +++++++++++++++++- .../Solution.cpp | 27 ++++ .../Solution.go | 26 ++++ .../Solution.java | 27 ++++ .../Solution.py | 22 +++ .../Solution.ts | 22 +++ .../3340.Check Balanced String/README.md | 2 + .../3340.Check Balanced String/README_EN.md | 2 + .../README.md | 6 + .../README_EN.md | 6 + .../README.md | 6 + .../README_EN.md | 6 + .../README.md | 5 + .../README_EN.md | 5 + solution/README.md | 8 +- solution/README_EN.md | 8 +- 21 files changed, 458 insertions(+), 30 deletions(-) create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.cpp create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.go create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.java create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.py create mode 100644 solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.ts diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" index 3b7816d980322..91a2164a9c7f8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" @@ -230,11 +230,11 @@ class Solution { if n < 2 { return n } - + let sortedNums = Array(Set(nums)).sorted() var ans = 1 var currentStreak = 1 - + for i in 1.. Int { let numSet: Set = Set(nums) var longestStreak = 0 - + for num in nums { if !numSet.contains(num - 1) { var currentNum = num var currentStreak = 1 - + while numSet.contains(currentNum + 1) { currentNum += 1 currentStreak += 1 } - + longestStreak = max(longestStreak, currentStreak) } } - + return longestStreak } } diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md index 3571085f71978..cc693068538e3 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md @@ -88,7 +88,7 @@ tags: 我们用两个变量 $\textit{cnt0}$ 和 $\textit{cnt1}$ 分别记录当前窗口内的 $0$ 和 $1$ 的个数,用 $\textit{ans}$ 记录满足 $k$ 约束的子字符串的个数,用 $l$ 记录窗口的左边界。 -当我们右移窗口时,如果窗口内的 $0$ 和 $1$ 的个数都大于 $k$,我们就需要左移窗口,直到窗口内的 $0$ 和 $1$ 的个数都不大于 $k$。此时,窗口内的所有子字符串都满足 $k$ 约束,个数为 $r - l + 1$,其中 $r$ 是窗口的右边界。我们将这个个数累加到 $\textit{ans}$ 中。 +当我们右移窗口时,如果窗口内的 $0$ 和 $1$ 的个数都大于 $k$,我们就需要左移窗口,直到窗口内的 $0$ 和 $1$ 的个数都不大于 $k$。此时,窗口内所有以 $r$ 作为右端点的子字符串都满足 $k$ 约束,个数为 $r - l + 1$,其中 $r$ 是窗口的右边界。我们将这个个数累加到 $\textit{ans}$ 中。 最后,我们返回 $\textit{ans}$ 即可。 @@ -175,9 +175,9 @@ function countKConstraintSubstrings(s: string, k: number): number { const cnt: [number, number] = [0, 0]; let [ans, l] = [0, 0]; for (let r = 0; r < s.length; ++r) { - cnt[s.charCodeAt(r) - 48]++; + cnt[+s[r]]++; while (cnt[0] > k && cnt[1] > k) { - cnt[s.charCodeAt(l++) - 48]--; + cnt[+s[l++]]--; } ans += r - l + 1; } diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md index c5e7a28f4fbde..8366c28a28b09 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md @@ -173,9 +173,9 @@ function countKConstraintSubstrings(s: string, k: number): number { const cnt: [number, number] = [0, 0]; let [ans, l] = [0, 0]; for (let r = 0; r < s.length; ++r) { - cnt[s.charCodeAt(r) - 48]++; + cnt[+s[r]]++; while (cnt[0] > k && cnt[1] > k) { - cnt[s.charCodeAt(l++) - 48]--; + cnt[+s[l++]]--; } ans += r - l + 1; } diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts index d25347e7b260d..ffa5bcbb898a2 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts @@ -2,9 +2,9 @@ function countKConstraintSubstrings(s: string, k: number): number { const cnt: [number, number] = [0, 0]; let [ans, l] = [0, 0]; for (let r = 0; r < s.length; ++r) { - cnt[s.charCodeAt(r) - 48]++; + cnt[+s[r]]++; while (cnt[0] > k && cnt[1] > k) { - cnt[s.charCodeAt(l++) - 48]--; + cnt[+s[l++]]--; } ans += r - l + 1; } diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md index 052df1d9b8ac0..dd70b880ff342 100644 --- a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README.md @@ -81,32 +81,165 @@ tags: -### 方法一 +### 方法一:滑动窗口 + 前缀和 + +我们用两个变量 $\textit{cnt0}$ 和 $\textit{cnt1}$ 分别记录当前窗口内的 $0$ 和 $1$ 的个数,指针 $i$ 和 $j$ 分别标识窗口的左右边界。用一个数组 $d$ 记录每个位置 $i$ 右边第一个不满足 $k$ 约束的位置,初始时 $d[i] = n$。另外,用一个长度为 $n + 1$ 的前缀和数组 $\textit{pre}[i]$ 记录以前 $i$ 个位置作为右边界的满足 $k$ 约束的子字符串的个数。 + +当我们右移窗口时,如果窗口内的 $0$ 和 $1$ 的个数都大于 $k$,我们将 $d[i]$ 更新为 $j$,表示位置 $i$ 右边第一个不满足 $k$ 约束的位置。然后我们将 $i$ 右移一位,直到窗口内的 $0$ 和 $1$ 的个数都不大于 $k$。此时,我们可以计算出以 $j$ 为右边界的满足 $k$ 约束的子字符串的个数,即 $j - i + 1$,我们更新到前缀和数组中。 + +最后,对于每个查询 $[l, r]$,我们首先找出 $l$ 右边第一个不满足 $k$ 约束的位置 $p$,那么 $p = \min(r + 1, d[l])$,那么 $[l, p - 1]$ 的所有子字符串都满足 $k$ 约束,个数为 $(1 + p - l) \times (p - l) / 2$,然后,我们计算以 $[p, r]$ 为右边界的满足 $k$ 约束的子字符串的个数,即 $\textit{pre}[r + 1] - \textit{pre}[p]$,最后将两者相加即可。 + +时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度和查询数组 $\textit{queries}$ 的长度。 #### Python3 ```python - +class Solution: + def countKConstraintSubstrings( + self, s: str, k: int, queries: List[List[int]] + ) -> List[int]: + cnt = [0, 0] + i, n = 0, len(s) + d = [n] * n + pre = [0] * (n + 1) + for j, x in enumerate(map(int, s)): + cnt[x] += 1 + while cnt[0] > k and cnt[1] > k: + d[i] = j + cnt[int(s[i])] -= 1 + i += 1 + pre[j + 1] = pre[j] + j - i + 1 + ans = [] + for l, r in queries: + p = min(r + 1, d[l]) + a = (1 + p - l) * (p - l) // 2 + b = pre[r + 1] - pre[p] + ans.append(a + b) + return ans ``` #### Java ```java - +class Solution { + public long[] countKConstraintSubstrings(String s, int k, int[][] queries) { + int[] cnt = new int[2]; + int n = s.length(); + int[] d = new int[n]; + Arrays.fill(d, n); + long[] pre = new long[n + 1]; + for (int i = 0, j = 0; j < n; ++j) { + cnt[s.charAt(j) - '0']++; + while (cnt[0] > k && cnt[1] > k) { + d[i] = j; + cnt[s.charAt(i++) - '0']--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + int m = queries.length; + long[] ans = new long[m]; + for (int i = 0; i < m; ++i) { + int l = queries[i][0], r = queries[i][1]; + int p = Math.min(r + 1, d[l]); + long a = (1L + p - l) * (p - l) / 2; + long b = pre[r + 1] - pre[p]; + ans[i] = a + b; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector countKConstraintSubstrings(string s, int k, vector>& queries) { + int cnt[2]{}; + int n = s.size(); + vector d(n, n); + long long pre[n + 1]; + pre[0] = 0; + for (int i = 0, j = 0; j < n; ++j) { + cnt[s[j] - '0']++; + while (cnt[0] > k && cnt[1] > k) { + d[i] = j; + cnt[s[i++] - '0']--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + vector ans; + for (const auto& q : queries) { + int l = q[0], r = q[1]; + int p = min(r + 1, d[l]); + long long a = (1LL + p - l) * (p - l) / 2; + long long b = pre[r + 1] - pre[p]; + ans.push_back(a + b); + } + return ans; + } +}; ``` #### Go ```go +func countKConstraintSubstrings(s string, k int, queries [][]int) (ans []int64) { + cnt := [2]int{} + n := len(s) + d := make([]int, n) + for i := range d { + d[i] = n + } + pre := make([]int, n+1) + for i, j := 0, 0; j < n; j++ { + cnt[s[j]-'0']++ + for cnt[0] > k && cnt[1] > k { + d[i] = j + cnt[s[i]-'0']-- + i++ + } + pre[j+1] = pre[j] + j - i + 1 + } + for _, q := range queries { + l, r := q[0], q[1] + p := min(r+1, d[l]) + a := (1 + p - l) * (p - l) / 2 + b := pre[r+1] - pre[p] + ans = append(ans, int64(a+b)) + } + return +} +``` +#### TypeScript + +```ts +function countKConstraintSubstrings(s: string, k: number, queries: number[][]): number[] { + const cnt: [number, number] = [0, 0]; + const n = s.length; + const d: number[] = Array(n).fill(n); + const pre: number[] = Array(n + 1).fill(0); + for (let i = 0, j = 0; j < n; ++j) { + cnt[+s[j]]++; + while (Math.min(cnt[0], cnt[1]) > k) { + d[i] = j; + cnt[+s[i++]]--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + const ans: number[] = []; + for (const [l, r] of queries) { + const p = Math.min(r + 1, d[l]); + const a = ((1 + p - l) * (p - l)) / 2; + const b = pre[r + 1] - pre[p]; + ans.push(a + b); + } + return ans; +} ``` diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md index 19be3a66efeba..46a65e602c9ac 100644 --- a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/README_EN.md @@ -79,32 +79,165 @@ tags: -### Solution 1 +### Solution 1: Sliding Window + Prefix Sum + +We use two variables $\textit{cnt0}$ and $\textit{cnt1}$ to record the number of $0$s and $1$s in the current window, respectively. Pointers $i$ and $j$ mark the left and right boundaries of the window. We use an array $d$ to record the first position to the right of each position $i$ that does not satisfy the $k$ constraint, initially setting $d[i] = n$. Additionally, we use a prefix sum array $\textit{pre}[i]$ of length $n + 1$ to record the number of substrings that satisfy the $k$ constraint with the right boundary at position $i$. + +When we move the window to the right, if the number of $0$s and $1$s in the window both exceed $k$, we update $d[i]$ to $j$, indicating that the first position to the right of $i$ that does not satisfy the $k$ constraint is $j$. Then we move $i$ one position to the right until the number of $0$s and $1$s in the window are both less than or equal to $k$. At this point, we can calculate the number of substrings that satisfy the $k$ constraint with the right boundary at $j$, which is $j - i + 1$, and update this in the prefix sum array. + +Finally, for each query $[l, r]$, we first find the first position $p$ to the right of $l$ that does not satisfy the $k$ constraint, which is $p = \min(r + 1, d[l])$. All substrings in the range $[l, p - 1]$ satisfy the $k$ constraint, and the number of such substrings is $(1 + p - l) \times (p - l) / 2$. Then, we calculate the number of substrings that satisfy the $k$ constraint with the right boundary in the range $[p, r]$, which is $\textit{pre}[r + 1] - \textit{pre}[p]$. Finally, we add the two results together. + +The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the string $s$ and the query array $\textit{queries}$, respectively. #### Python3 ```python - +class Solution: + def countKConstraintSubstrings( + self, s: str, k: int, queries: List[List[int]] + ) -> List[int]: + cnt = [0, 0] + i, n = 0, len(s) + d = [n] * n + pre = [0] * (n + 1) + for j, x in enumerate(map(int, s)): + cnt[x] += 1 + while cnt[0] > k and cnt[1] > k: + d[i] = j + cnt[int(s[i])] -= 1 + i += 1 + pre[j + 1] = pre[j] + j - i + 1 + ans = [] + for l, r in queries: + p = min(r + 1, d[l]) + a = (1 + p - l) * (p - l) // 2 + b = pre[r + 1] - pre[p] + ans.append(a + b) + return ans ``` #### Java ```java - +class Solution { + public long[] countKConstraintSubstrings(String s, int k, int[][] queries) { + int[] cnt = new int[2]; + int n = s.length(); + int[] d = new int[n]; + Arrays.fill(d, n); + long[] pre = new long[n + 1]; + for (int i = 0, j = 0; j < n; ++j) { + cnt[s.charAt(j) - '0']++; + while (cnt[0] > k && cnt[1] > k) { + d[i] = j; + cnt[s.charAt(i++) - '0']--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + int m = queries.length; + long[] ans = new long[m]; + for (int i = 0; i < m; ++i) { + int l = queries[i][0], r = queries[i][1]; + int p = Math.min(r + 1, d[l]); + long a = (1L + p - l) * (p - l) / 2; + long b = pre[r + 1] - pre[p]; + ans[i] = a + b; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector countKConstraintSubstrings(string s, int k, vector>& queries) { + int cnt[2]{}; + int n = s.size(); + vector d(n, n); + long long pre[n + 1]; + pre[0] = 0; + for (int i = 0, j = 0; j < n; ++j) { + cnt[s[j] - '0']++; + while (cnt[0] > k && cnt[1] > k) { + d[i] = j; + cnt[s[i++] - '0']--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + vector ans; + for (const auto& q : queries) { + int l = q[0], r = q[1]; + int p = min(r + 1, d[l]); + long long a = (1LL + p - l) * (p - l) / 2; + long long b = pre[r + 1] - pre[p]; + ans.push_back(a + b); + } + return ans; + } +}; ``` #### Go ```go +func countKConstraintSubstrings(s string, k int, queries [][]int) (ans []int64) { + cnt := [2]int{} + n := len(s) + d := make([]int, n) + for i := range d { + d[i] = n + } + pre := make([]int, n+1) + for i, j := 0, 0; j < n; j++ { + cnt[s[j]-'0']++ + for cnt[0] > k && cnt[1] > k { + d[i] = j + cnt[s[i]-'0']-- + i++ + } + pre[j+1] = pre[j] + j - i + 1 + } + for _, q := range queries { + l, r := q[0], q[1] + p := min(r+1, d[l]) + a := (1 + p - l) * (p - l) / 2 + b := pre[r+1] - pre[p] + ans = append(ans, int64(a+b)) + } + return +} +``` +#### TypeScript + +```ts +function countKConstraintSubstrings(s: string, k: number, queries: number[][]): number[] { + const cnt: [number, number] = [0, 0]; + const n = s.length; + const d: number[] = Array(n).fill(n); + const pre: number[] = Array(n + 1).fill(0); + for (let i = 0, j = 0; j < n; ++j) { + cnt[+s[j]]++; + while (Math.min(cnt[0], cnt[1]) > k) { + d[i] = j; + cnt[+s[i++]]--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + const ans: number[] = []; + for (const [l, r] of queries) { + const p = Math.min(r + 1, d[l]); + const a = ((1 + p - l) * (p - l)) / 2; + const b = pre[r + 1] - pre[p]; + ans.push(a + b); + } + return ans; +} ``` diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.cpp b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.cpp new file mode 100644 index 0000000000000..c7133e2b4d277 --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector countKConstraintSubstrings(string s, int k, vector>& queries) { + int cnt[2]{}; + int n = s.size(); + vector d(n, n); + long long pre[n + 1]; + pre[0] = 0; + for (int i = 0, j = 0; j < n; ++j) { + cnt[s[j] - '0']++; + while (cnt[0] > k && cnt[1] > k) { + d[i] = j; + cnt[s[i++] - '0']--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + vector ans; + for (const auto& q : queries) { + int l = q[0], r = q[1]; + int p = min(r + 1, d[l]); + long long a = (1LL + p - l) * (p - l) / 2; + long long b = pre[r + 1] - pre[p]; + ans.push_back(a + b); + } + return ans; + } +}; diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.go b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.go new file mode 100644 index 0000000000000..a4d771c56cd36 --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.go @@ -0,0 +1,26 @@ +func countKConstraintSubstrings(s string, k int, queries [][]int) (ans []int64) { + cnt := [2]int{} + n := len(s) + d := make([]int, n) + for i := range d { + d[i] = n + } + pre := make([]int, n+1) + for i, j := 0, 0; j < n; j++ { + cnt[s[j]-'0']++ + for cnt[0] > k && cnt[1] > k { + d[i] = j + cnt[s[i]-'0']-- + i++ + } + pre[j+1] = pre[j] + j - i + 1 + } + for _, q := range queries { + l, r := q[0], q[1] + p := min(r+1, d[l]) + a := (1 + p - l) * (p - l) / 2 + b := pre[r+1] - pre[p] + ans = append(ans, int64(a+b)) + } + return +} diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.java b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.java new file mode 100644 index 0000000000000..a4bc2039a7a11 --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.java @@ -0,0 +1,27 @@ +class Solution { + public long[] countKConstraintSubstrings(String s, int k, int[][] queries) { + int[] cnt = new int[2]; + int n = s.length(); + int[] d = new int[n]; + Arrays.fill(d, n); + long[] pre = new long[n + 1]; + for (int i = 0, j = 0; j < n; ++j) { + cnt[s.charAt(j) - '0']++; + while (cnt[0] > k && cnt[1] > k) { + d[i] = j; + cnt[s.charAt(i++) - '0']--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + int m = queries.length; + long[] ans = new long[m]; + for (int i = 0; i < m; ++i) { + int l = queries[i][0], r = queries[i][1]; + int p = Math.min(r + 1, d[l]); + long a = (1L + p - l) * (p - l) / 2; + long b = pre[r + 1] - pre[p]; + ans[i] = a + b; + } + return ans; + } +} diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.py b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.py new file mode 100644 index 0000000000000..7a89ac8498c25 --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.py @@ -0,0 +1,22 @@ +class Solution: + def countKConstraintSubstrings( + self, s: str, k: int, queries: List[List[int]] + ) -> List[int]: + cnt = [0, 0] + i, n = 0, len(s) + d = [n] * n + pre = [0] * (n + 1) + for j, x in enumerate(map(int, s)): + cnt[x] += 1 + while cnt[0] > k and cnt[1] > k: + d[i] = j + cnt[int(s[i])] -= 1 + i += 1 + pre[j + 1] = pre[j] + j - i + 1 + ans = [] + for l, r in queries: + p = min(r + 1, d[l]) + a = (1 + p - l) * (p - l) // 2 + b = pre[r + 1] - pre[p] + ans.append(a + b) + return ans diff --git a/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.ts b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.ts new file mode 100644 index 0000000000000..1294ccc738b8d --- /dev/null +++ b/solution/3200-3299/3261.Count Substrings That Satisfy K-Constraint II/Solution.ts @@ -0,0 +1,22 @@ +function countKConstraintSubstrings(s: string, k: number, queries: number[][]): number[] { + const cnt: [number, number] = [0, 0]; + const n = s.length; + const d: number[] = Array(n).fill(n); + const pre: number[] = Array(n + 1).fill(0); + for (let i = 0, j = 0; j < n; ++j) { + cnt[+s[j]]++; + while (Math.min(cnt[0], cnt[1]) > k) { + d[i] = j; + cnt[+s[i++]]--; + } + pre[j + 1] = pre[j] + j - i + 1; + } + const ans: number[] = []; + for (const [l, r] of queries) { + const p = Math.min(r + 1, d[l]); + const a = ((1 + p - l) * (p - l)) / 2; + const b = pre[r + 1] - pre[p]; + ans.push(a + b); + } + return ans; +} diff --git a/solution/3300-3399/3340.Check Balanced String/README.md b/solution/3300-3399/3340.Check Balanced String/README.md index 2660a752c8c10..f107c94d4713e 100644 --- a/solution/3300-3399/3340.Check Balanced String/README.md +++ b/solution/3300-3399/3340.Check Balanced String/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3340.Check%20Balanced%20String/README.md +tags: + - 字符串 --- diff --git a/solution/3300-3399/3340.Check Balanced String/README_EN.md b/solution/3300-3399/3340.Check Balanced String/README_EN.md index 13721179effa8..f174facb93812 100644 --- a/solution/3300-3399/3340.Check Balanced String/README_EN.md +++ b/solution/3300-3399/3340.Check Balanced String/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3340.Check%20Balanced%20String/README_EN.md +tags: + - String --- diff --git a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md index a63a738d8bede..364565a827492 100644 --- a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md +++ b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3341.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20I/README.md +tags: + - 图 + - 数组 + - 矩阵 + - 最短路 + - 堆(优先队列) --- diff --git a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md index 2474d0bd796fc..f44c667220e08 100644 --- a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md +++ b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3341.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20I/README_EN.md +tags: + - Graph + - Array + - Matrix + - Shortest Path + - Heap (Priority Queue) --- diff --git a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md index 3f9fb6f5ab040..c34481b8bc319 100644 --- a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md +++ b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3342.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20II/README.md +tags: + - 图 + - 数组 + - 矩阵 + - 最短路 + - 堆(优先队列) --- diff --git a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md index 6c6a51e01310d..8fc2a3b39e2fa 100644 --- a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md +++ b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3342.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20II/README_EN.md +tags: + - Graph + - Array + - Matrix + - Shortest Path + - Heap (Priority Queue) --- diff --git a/solution/3300-3399/3343.Count Number of Balanced Permutations/README.md b/solution/3300-3399/3343.Count Number of Balanced Permutations/README.md index 4496f3905e724..cb84d730105ab 100644 --- a/solution/3300-3399/3343.Count Number of Balanced Permutations/README.md +++ b/solution/3300-3399/3343.Count Number of Balanced Permutations/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3343.Count%20Number%20of%20Balanced%20Permutations/README.md +tags: + - 数学 + - 字符串 + - 动态规划 + - 组合数学 --- diff --git a/solution/3300-3399/3343.Count Number of Balanced Permutations/README_EN.md b/solution/3300-3399/3343.Count Number of Balanced Permutations/README_EN.md index 0e6f12e498222..1ca3211a9522e 100644 --- a/solution/3300-3399/3343.Count Number of Balanced Permutations/README_EN.md +++ b/solution/3300-3399/3343.Count Number of Balanced Permutations/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3343.Count%20Number%20of%20Balanced%20Permutations/README_EN.md +tags: + - Math + - String + - Dynamic Programming + - Combinatorics --- diff --git a/solution/README.md b/solution/README.md index 2ab272d558016..4e29eb4c172b4 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3350,10 +3350,10 @@ | 3337 | [字符串转换后的长度 II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README.md) | `哈希表`,`数学`,`字符串`,`动态规划`,`计数` | 困难 | 第 421 场周赛 | | 3338 | [第二高的薪水 II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 | | 3339 | [查找 K 偶数数组的数量](/solution/3300-3399/3339.Find%20the%20Number%20of%20K-Even%20Arrays/README.md) | `动态规划` | 中等 | 🔒 | -| 3340 | [检查平衡字符串](/solution/3300-3399/3340.Check%20Balanced%20String/README.md) | | 简单 | 第 422 场周赛 | -| 3341 | [到达最后一个房间的最少时间 I](/solution/3300-3399/3341.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20I/README.md) | | 中等 | 第 422 场周赛 | -| 3342 | [到达最后一个房间的最少时间 II](/solution/3300-3399/3342.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20II/README.md) | | 中等 | 第 422 场周赛 | -| 3343 | [统计平衡排列的数目](/solution/3300-3399/3343.Count%20Number%20of%20Balanced%20Permutations/README.md) | | 困难 | 第 422 场周赛 | +| 3340 | [检查平衡字符串](/solution/3300-3399/3340.Check%20Balanced%20String/README.md) | `字符串` | 简单 | 第 422 场周赛 | +| 3341 | [到达最后一个房间的最少时间 I](/solution/3300-3399/3341.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20I/README.md) | `图`,`数组`,`矩阵`,`最短路`,`堆(优先队列)` | 中等 | 第 422 场周赛 | +| 3342 | [到达最后一个房间的最少时间 II](/solution/3300-3399/3342.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20II/README.md) | `图`,`数组`,`矩阵`,`最短路`,`堆(优先队列)` | 中等 | 第 422 场周赛 | +| 3343 | [统计平衡排列的数目](/solution/3300-3399/3343.Count%20Number%20of%20Balanced%20Permutations/README.md) | `数学`,`字符串`,`动态规划`,`组合数学` | 困难 | 第 422 场周赛 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index cd6bcb1db9f76..871f4f9731f5d 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3348,10 +3348,10 @@ Press Control + F(or Command + F on | 3337 | [Total Characters in String After Transformations II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README_EN.md) | `Hash Table`,`Math`,`String`,`Dynamic Programming`,`Counting` | Hard | Weekly Contest 421 | | 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 | | 3339 | [Find the Number of K-Even Arrays](/solution/3300-3399/3339.Find%20the%20Number%20of%20K-Even%20Arrays/README_EN.md) | `Dynamic Programming` | Medium | 🔒 | -| 3340 | [Check Balanced String](/solution/3300-3399/3340.Check%20Balanced%20String/README_EN.md) | | Easy | Weekly Contest 422 | -| 3341 | [Find Minimum Time to Reach Last Room I](/solution/3300-3399/3341.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20I/README_EN.md) | | Medium | Weekly Contest 422 | -| 3342 | [Find Minimum Time to Reach Last Room II](/solution/3300-3399/3342.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20II/README_EN.md) | | Medium | Weekly Contest 422 | -| 3343 | [Count Number of Balanced Permutations](/solution/3300-3399/3343.Count%20Number%20of%20Balanced%20Permutations/README_EN.md) | | Hard | Weekly Contest 422 | +| 3340 | [Check Balanced String](/solution/3300-3399/3340.Check%20Balanced%20String/README_EN.md) | `String` | Easy | Weekly Contest 422 | +| 3341 | [Find Minimum Time to Reach Last Room I](/solution/3300-3399/3341.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20I/README_EN.md) | `Graph`,`Array`,`Matrix`,`Shortest Path`,`Heap (Priority Queue)` | Medium | Weekly Contest 422 | +| 3342 | [Find Minimum Time to Reach Last Room II](/solution/3300-3399/3342.Find%20Minimum%20Time%20to%20Reach%20Last%20Room%20II/README_EN.md) | `Graph`,`Array`,`Matrix`,`Shortest Path`,`Heap (Priority Queue)` | Medium | Weekly Contest 422 | +| 3343 | [Count Number of Balanced Permutations](/solution/3300-3399/3343.Count%20Number%20of%20Balanced%20Permutations/README_EN.md) | `Math`,`String`,`Dynamic Programming`,`Combinatorics` | Hard | Weekly Contest 422 | ## Copyright