Skip to content

Commit 3c3f8bb

Browse files
committed
update: 添加问题“2537.统计好子数组的数目”的代码和题解(#878)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 98860b0 commit 3c3f8bb

8 files changed

+320
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-16 19:45:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-16 20:25:23
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
13+
class Solution {
14+
public:
15+
ll countGood(vector<int>& nums, int k) {
16+
ll ans = 0;
17+
unordered_map<int, int> ma;
18+
ll now = 0;
19+
for (int l = 0, r = 0; r < nums.size(); r++) {
20+
now += ma[nums[r]]++;
21+
while (now >= k) {
22+
now -= --ma[nums[l++]];
23+
}
24+
ans += l;
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-16 20:50:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-16 20:56:36
6+
*/
7+
package main
8+
9+
func countGood(nums []int, k int) (ans int64) {
10+
times := map[int]int64{}
11+
now := int64(0)
12+
for l, r := 0, 0; r < len(nums); r++ {
13+
now += times[nums[r]]
14+
times[nums[r]]++
15+
for now >= int64(k) {
16+
times[nums[l]]--
17+
now -= times[nums[l]]
18+
l++
19+
}
20+
ans += int64(l)
21+
}
22+
return
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-16 20:27:13
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-16 20:49:27
6+
*/
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
10+
// // CE
11+
// class Solution {
12+
// public long countGood(int[] nums, int k) {
13+
// long ans = 0, now = 0;
14+
// Map<Integer, Integer> ma = new HashMap<>();
15+
// for (int l = 0, r = 0; r < nums.length; r++) {
16+
// now += ma[nums[r]]++;
17+
// while (now >= k) {
18+
// now -= --ma[nums[l++]];
19+
// }
20+
// ans += now;
21+
// }
22+
// return ans;
23+
// }
24+
// }
25+
26+
class Solution {
27+
public long countGood(int[] nums, int k) {
28+
long ans = 0, now = 0;
29+
Map<Integer, Integer> ma = new HashMap<>();
30+
for (int l = 0, r = 0; r < nums.length; r++) {
31+
now += ma.merge(nums[r], 1, Integer::sum) - 1;
32+
while (now >= k) {
33+
now -= ma.merge(nums[l++], -1, Integer::sum);
34+
}
35+
ans += l;
36+
}
37+
return ans;
38+
}
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-16 20:23:21
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-16 20:26:37
6+
'''
7+
from typing import List
8+
from collections import defaultdict
9+
10+
11+
class Solution:
12+
def countGood(self, nums: List[int], k: int) -> int:
13+
times = defaultdict(int)
14+
ans = l = now = 0
15+
for t in nums:
16+
now += times[t]
17+
times[t] += 1
18+
while now >= k:
19+
times[nums[l]] -= 1
20+
now -= times[nums[l]]
21+
l += 1
22+
ans += l
23+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@
769769
|2529.正整数和负整数的最大计数|简单|<a href="https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/09/LeetCode%202529.%E6%AD%A3%E6%95%B4%E6%95%B0%E5%92%8C%E8%B4%9F%E6%95%B4%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E8%AE%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137554489" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer/solutions/2731345/letmefly-2529zheng-zheng-shu-he-fu-zheng-p367/" target="_blank">LeetCode题解</a>|
770770
|2530.执行K次操作后的最大分数|中等|<a href="https://leetcode.cn/problems/maximal-score-after-applying-k-operations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/18/LeetCode%202530.%E6%89%A7%E8%A1%8CK%E6%AC%A1%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E6%9C%80%E5%A4%A7%E5%88%86%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133899145" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximal-score-after-applying-k-operations/solutions/2487511/letmefly-2530zhi-xing-k-ci-cao-zuo-hou-d-s4pm/" target="_blank">LeetCode题解</a>|
771771
|2535.数组元素和与数字和的绝对差|简单|<a href="https://leetcode.cn/problems/difference-between-element-sum-and-digit-sum-of-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/26/LeetCode%202535.%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E5%92%8C%E4%B8%8E%E6%95%B0%E5%AD%97%E5%92%8C%E7%9A%84%E7%BB%9D%E5%AF%B9%E5%B7%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142568318" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/difference-between-element-sum-and-digit-sum-of-an-array/solutions/2931399/letmefly-2535shu-zu-yuan-su-he-yu-shu-zi-lp8o/" target="_blank">LeetCode题解</a>|
772+
|2537.统计好子数组的数目|中等|<a href="https://leetcode.cn/problems/count-the-number-of-good-subarrays/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/16/LeetCode%202537.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147288778" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-good-subarrays/solutions/3653508/letmefly-2537tong-ji-hao-zi-shu-zu-de-sh-9fmo/" target="_blank">LeetCode题解</a>|
772773
|2544.交替数字和|简单|<a href="https://leetcode.cn/problems/alternating-digit-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/12/LeetCode%202544.%E4%BA%A4%E6%9B%BF%E6%95%B0%E5%AD%97%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131673485" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/2340450/letmefly-2544jiao-ti-shu-zi-he-by-tisfy-aa0f/" target="_blank">LeetCode题解</a>|
773774
|2545.根据第K场考试的分数排序|中等|<a href="https://leetcode.cn/problems/sort-the-students-by-their-kth-score/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/21/LeetCode%202545.%E6%A0%B9%E6%8D%AE%E7%AC%ACK%E5%9C%BA%E8%80%83%E8%AF%95%E7%9A%84%E5%88%86%E6%95%B0%E6%8E%92%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144635776" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sort-the-students-by-their-kth-score/solutions/3026570/letmefly-2545gen-ju-di-k-chang-kao-shi-d-rp50/" target="_blank">LeetCode题解</a>|
774775
|2549.统计桌面上的不同数字|简单|<a href="https://leetcode.cn/problems/count-distinct-numbers-on-board/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/23/LeetCode%202549.%E7%BB%9F%E8%AE%A1%E6%A1%8C%E9%9D%A2%E4%B8%8A%E7%9A%84%E4%B8%8D%E5%90%8C%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136963667" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-distinct-numbers-on-board/solutions/2703112/letmefly-2549tong-ji-zhuo-mian-shang-de-992xw/" target="_blank">LeetCode题解</a>|
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
---
2+
title: 2537.统计好子数组的数目:滑动窗口(双指针)
3+
date: 2025-04-16 21:01:12
4+
tags: [题解, LeetCode, 中等, 数组, 哈希表, 滑动窗口, 双指针]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2537.统计好子数组的数目:滑动窗口(双指针)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-the-number-of-good-subarrays/](https://leetcode.cn/problems/count-the-number-of-good-subarrays/)
11+
12+
<p>给你一个整数数组 <code>nums</code>&nbsp;和一个整数 <code>k</code>&nbsp;,请你返回 <code>nums</code>&nbsp;中 <strong>好</strong>&nbsp;子数组的数目。</p>
13+
14+
<p>一个子数组 <code>arr</code>&nbsp;如果有 <strong>至少</strong>&nbsp;<code>k</code>&nbsp;对下标 <code>(i, j)</code>&nbsp;满足 <code>i &lt; j</code>&nbsp;且 <code>arr[i] == arr[j]</code>&nbsp;,那么称它是一个 <strong>好</strong>&nbsp;子数组。</p>
15+
16+
<p><strong>子数组</strong>&nbsp;是原数组中一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre><b>输入:</b>nums = [1,1,1,1,1], k = 10
23+
<b>输出:</b>1
24+
<b>解释:</b>唯一的好子数组是这个数组本身。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre><b>输入:</b>nums = [3,1,4,3,2,2,4], k = 2
30+
<b>输出:</b>4
31+
<b>解释:</b>总共有 4 个不同的好子数组:
32+
- [3,1,4,3,2,2] 有 2 对。
33+
- [3,1,4,3,2,2,4] 有 3 对。
34+
- [1,4,3,2,2,4] 有 2 对。
35+
- [4,3,2,2,4] 有 2 对。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
44+
<li><code>1 &lt;= nums[i], k &lt;= 10<sup>9</sup></code></li>
45+
</ul>
46+
47+
48+
49+
## 解题方法:滑动窗口(双指针)
50+
51+
### 解题思路
52+
53+
使用一个哈希表记录窗口(两个指针之间)中每个元素出现的次数。
54+
55+
右指针从第一个元素到最后一个元素遍历数组,在移动的过程中,将指向的元素添加到“窗口”中并更新窗口中的“相等对数”;
56+
57+
在窗口中的“相等对数”大于等于$k$时,不断右移左指针(将左指针指向元素移出窗口)并更新窗口中的“相等对数”;
58+
59+
这样,右指针每次移动一个位置,左指针指向的就是第一个“窗口中相等对数小于$k$”的位置。
60+
61+
### 具体方法
62+
63+
假设右移****指针新**加入**窗口中了一个元素$x$,那么如何更新“窗口中的相等对数”呢?
64+
65+
> **新加入**的$x$与之前窗口中的每个**已有**$x$都能配对,所以新增“对数”为新元素**加入前**哈希表中$x$的个数。
66+
67+
假设右移****指针新**移除**了窗口中一个元素$x$,那么如何更新“窗口中的相等对数”呢?
68+
69+
> **被移除**的$x$与窗口中的每个**其他**$x$都配过对,所以减少的“对数”为新元素**移除后**哈希表中$x$的个数。
70+
71+
假设第一个“使窗口中相等对数小于$k$”的左指针下标为$l$,那么答案应该增加几?
72+
73+
> $l$下标左边的任何一个元素开始,到右指针$r$的字数组都满足“相等对数大于等于$k$”,都是“好字数组”。
74+
>
75+
> 以$r$结尾的好字数组的个数为$l$左边的元素个数,对答案的贡献为$l$。
76+
77+
### 时空复杂度
78+
79+
+ 时间复杂度$O(len(nums))$。左右指针最多各自遍历数组一次
80+
+ 空间复杂度$O(1)$
81+
82+
### AC代码
83+
84+
#### C++
85+
86+
```cpp
87+
/*
88+
* @Author: LetMeFly
89+
* @Date: 2025-04-16 19:45:53
90+
* @LastEditors: LetMeFly.xyz
91+
* @LastEditTime: 2025-04-16 20:25:23
92+
*/
93+
typedef long long ll;
94+
95+
class Solution {
96+
public:
97+
ll countGood(vector<int>& nums, int k) {
98+
ll ans = 0;
99+
unordered_map<int, int> ma;
100+
ll now = 0;
101+
for (int l = 0, r = 0; r < nums.size(); r++) {
102+
now += ma[nums[r]]++;
103+
while (now >= k) {
104+
now -= --ma[nums[l++]];
105+
}
106+
ans += l;
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
#### Python
114+
115+
```python
116+
'''
117+
Author: LetMeFly
118+
Date: 2025-04-16 20:23:21
119+
LastEditors: LetMeFly.xyz
120+
LastEditTime: 2025-04-16 20:26:37
121+
'''
122+
from typing import List
123+
from collections import defaultdict
124+
125+
126+
class Solution:
127+
def countGood(self, nums: List[int], k: int) -> int:
128+
times = defaultdict(int)
129+
ans = l = now = 0
130+
for t in nums:
131+
now += times[t]
132+
times[t] += 1
133+
while now >= k:
134+
times[nums[l]] -= 1
135+
now -= times[nums[l]]
136+
l += 1
137+
ans += l
138+
return ans
139+
```
140+
141+
#### Java
142+
143+
```java
144+
/*
145+
* @Author: LetMeFly
146+
* @Date: 2025-04-16 20:27:13
147+
* @LastEditors: LetMeFly.xyz
148+
* @LastEditTime: 2025-04-16 20:49:27
149+
*/
150+
import java.util.Map;
151+
import java.util.HashMap;
152+
153+
class Solution {
154+
public long countGood(int[] nums, int k) {
155+
long ans = 0, now = 0;
156+
Map<Integer, Integer> ma = new HashMap<>();
157+
for (int l = 0, r = 0; r < nums.length; r++) {
158+
now += ma.merge(nums[r], 1, Integer::sum) - 1;
159+
while (now >= k) {
160+
now -= ma.merge(nums[l++], -1, Integer::sum);
161+
}
162+
ans += l;
163+
}
164+
return ans;
165+
}
166+
}
167+
```
168+
169+
#### Go
170+
171+
```go
172+
/*
173+
* @Author: LetMeFly
174+
* @Date: 2025-04-16 20:50:11
175+
* @LastEditors: LetMeFly.xyz
176+
* @LastEditTime: 2025-04-16 20:56:36
177+
*/
178+
package main
179+
180+
func countGood(nums []int, k int) (ans int64) {
181+
times := map[int]int64{}
182+
now := int64(0)
183+
for l, r := 0, 0; r < len(nums); r++ {
184+
now += times[nums[r]]
185+
times[nums[r]]++
186+
for now >= int64(k) {
187+
times[nums[l]]--
188+
now -= times[nums[l]]
189+
l++
190+
}
191+
ans += int64(l)
192+
}
193+
return
194+
}
195+
```
196+
197+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147288778)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/16/LeetCode%202537.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/)哦~
198+
>
199+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,13 @@ categories: [自用]
10961096
|||
10971097
|voucher|n. 保证人,证件,教育补助券,消费券<br/>v. 证明,为...准备凭据,发给...凭证|
10981098
|deterioration|n. 恶化,退化,变质,堕落|
1099+
|||
1100+
|wordy|adj. 话多的,冗长的,啰嗦的|
1101+
|oceanography|n. 海洋学|
1102+
|stronghold|n. 要塞,据点,堡垒,有广泛支持的地方|
1103+
|mackintosh|n. 防水胶布,胶布雨衣(麦金托什)|
1104+
|||
1105+
|ferrous|adj. 含铁的,铁的|
10991106

11001107
<p class="wordCounts">单词收录总数</p>
11011108

changes20250407.patch

-852 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)