Skip to content

feat: add solutions to lc problems: No.2839~2846 #1574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@

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

**方法一:计数**

我们观察题目中的操作,可以发现,如果字符串的两个下标 $i$ 和 $j$ 的奇偶性相同,那么它们可以通过交换改变顺序。

因此,我们可以统计两个字符串中奇数下标的字符的出现次数,以及偶数下标的字符的出现次数,如果两个字符串的统计结果相同,那么我们就可以通过操作使得两个字符串相等。

时间复杂度 $O(n + |\Sigma|)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $\Sigma$ 是字符集。

相似题目:

- [2840. 判断通过操作能否让字符串相等 II](/solution/2800-2899/2840.Check%20if%20Strings%20Can%20be%20Made%20Equal%20With%20Operations%20II/README.md)

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@

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

**方法一:计数**

我们观察题目中的操作,可以发现,如果字符串的两个下标 $i$ 和 $j$ 的奇偶性相同,那么它们可以通过交换改变顺序。

因此,我们可以统计两个字符串中奇数下标的字符的出现次数,以及偶数下标的字符的出现次数,如果两个字符串的统计结果相同,那么我们就可以通过操作使得两个字符串相等。

时间复杂度 $O(n + |\Sigma|)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $\Sigma$ 是字符集。

相似题目:

- [2839. 判断通过操作能否让字符串相等 I](/solution/2800-2899/2839.Check%20if%20Strings%20Can%20be%20Made%20Equal%20With%20Operations%20I/README.md)

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@

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

**方法一:滑动窗口 + 哈希表**

我们可以遍历数组 $nums$,维护一个大小为 $k$ 的窗口,用哈希表 $cnt$ 统计窗口中每个元素的出现次数,用变量 $s$ 统计窗口中所有元素的和。如果 $cnt$ 中不同元素的个数大于等于 $m$,那么我们就更新答案 $ans = \max(ans, s)$。

遍历结束后,返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 是数组的长度。

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ s 的 k 子序列为:

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

**方法一:贪心 + 组合数学**

我们先用哈希表 $f$ 统计字符串 $s$ 中每个字符的出现次数,即 $f[c]$ 表示字符 $c$ 在字符串 $s$ 中出现的次数。

由于 $k$ 子序列是字符串 $s$ 中一个长度为 $k$ 的子序列,且字符唯一,因此,如果 $f$ 中不同字符的个数小于 $k$,那么不存在 $k$ 子序列,直接返回 $0$ 即可。

否则,要使得 $k$ 子序列的美丽值最大,我们需要尽可能地让美丽值大的字符尽可能地多地出现在 $k$ 子序列中。因此,我们可以对 $f$ 中的值进行倒序排序,得到一个数组 $vs$。

我们记数组 $vs$ 第 $k$ 个字符的出现次数为 $val$,一共有 $x$ 个字符出现的次数为 $val$。

那么我们先找出出现次数大于 $val$ 的字符,将每个字符出现的次数相乘,得到初始答案 $ans$,剩余的需要选取的字符个数更新为 $k$。我们需要从 $x$ 个字符中选取 $k$ 个字符,因此答案需要乘上组合数 $C_x^k$,最后再乘上 $val^k$,即 $ans = ans \times C_x^k \times val^k$。

注意,这里需要用到快速幂,以及取模操作。

时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $\Sigma$ 是字符集。本题中字符集为小写字母,因此 $|\Sigma| = 26$。

<!-- tabs:start -->

### **Python3**
Expand Down
6 changes: 6 additions & 0 deletions solution/2800-2899/2843.Count Symmetric Integers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@

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

**方法一:枚举**

我们枚举 $[low, high]$ 中的每个整数 $x$,判断其是否是对称整数。如果是,那么答案 $ans$ 增加 $1$。

时间复杂度 $O(n \times \log m)$,空间复杂度 $O(\log m)$。其中 $n$ 是 $[low, high]$ 中整数的个数,而 $m$ 是题目中给出的最大整数。

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@

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

**方法一:记忆化搜索**

我们注意到,整数 $x$ 要能被 $25$ 整除,即 $x \bmod 25 = 0$。因此,我们可以设计一个函数 $dfs(i, k)$,表示从字符串 $num$ 的第 $i$ 位开始,且当前数字模 $25$ 的结果为 $k$ 的情况下,要使得数字变成特殊数字,最少需要删除多少位数字。那么答案为 $dfs(0, 0)$。

函数 $dfs(i, k)$ 的执行逻辑如下:

- 如果 $i = n$,即已经处理完字符串 $num$ 的所有位,那么如果 $k = 0$,则当前数字可以被 $25$ 整除,返回 $0$,否则返回 $n$;
- 否则,第 $i$ 位可以被删除,此时需要删除一位,即 $dfs(i + 1, k) + 1$;第 $i$ 位不删除,那么 $k$ 的值变为 $(k \times 10 + \textit{num}[i]) \bmod 25$,即 $dfs(i + 1, (k \times 10 + \textit{num}[i]) \bmod 25)$。取这两者的最小值即可。

为了防止重复计算,我们可以使用记忆化的方法优化时间复杂度。

时间复杂度 $O(n \times 25)$,空间复杂度 $O(n \times 25)$。其中 $n$ 是字符串 $num$ 的长度。

<!-- tabs:start -->

### **Python3**
Expand Down
14 changes: 14 additions & 0 deletions solution/2800-2899/2845.Count of Interesting Subarrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@

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

**方法一:哈希表 + 前缀和**

题目要求一个区间内满足 $nums[i] \bmod modulo = k$ 的索引 $i$ 的数量,我们可以将数组 $nums$ 转换为一个 $0-1$ 数组 $arr$,其中 $arr[i] = 1$ 表示 $nums[i] \bmod modulo = k$,否则 $arr[i] = 0$。

那么对于一个区间 $[l, r]$,我们可以通过前缀和数组 $s$ 来计算 $arr[l..r]$ 中 $1$ 的数量,即 $s[r] - s[l - 1]$,其中 $s[0] = 0$。

我们用哈希表 $cnt$ 记录前缀和 $s \bmod modulo$ 出现的次数,初始时 $cnt[0]=1$。

接下来,我们遍历数组 $arr$,计算前缀和 $s$,将 $(s-k) \bmod modulo$ 出现的次数累加到答案中,然后将 $s \bmod modulo$ 出现的次数加 $1$。

遍历结束后,返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png" style="width: 339px; height: 344px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2846.Minimum%20Edge%20Weight%20Equilibrium%20Queries%20in%20a%20Tree/images/graph-6-1.png" style="width: 339px; height: 344px;" />
<pre>
<strong>输入:</strong>n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
<strong>输出:</strong>[0,0,1,3]
Expand All @@ -34,7 +34,7 @@
</pre>

<p><strong class="example">示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png" style="width: 472px; height: 370px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2846.Minimum%20Edge%20Weight%20Equilibrium%20Queries%20in%20a%20Tree/images/graph-9-1.png" style="width: 472px; height: 370px;" />
<pre>
<strong>输入:</strong>n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
<strong>输出:</strong>[1,2,2,3]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png" style="width: 339px; height: 344px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2846.Minimum%20Edge%20Weight%20Equilibrium%20Queries%20in%20a%20Tree/images/graph-6-1.png" style="width: 339px; height: 344px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
<strong>Output:</strong> [0,0,1,3]
Expand All @@ -31,7 +31,7 @@ For each queries[i], it can be shown that answer[i] is the minimum number of ope
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png" style="width: 472px; height: 370px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2846.Minimum%20Edge%20Weight%20Equilibrium%20Queries%20in%20a%20Tree/images/graph-9-1.png" style="width: 472px; height: 370px;" />
<pre>
<strong>Input:</strong> n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
<strong>Output:</strong> [1,2,2,3]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.