Skip to content

Commit bb6a6ea

Browse files
authored
update: 添加问题“2799.统计完全子数组的数目”的代码和题解(#897)
* feat: revert.revert #879 (#896) b2da1d4 Signed-off-by: LetMeFly666 <[email protected]> * words: en words (#896) 5d8255b ( 7312920 ) + today Signed-off-by: LetMeFly666 <[email protected]> * docs: todo - 贝叶斯公式 (#896) Signed-off-by: LetMeFly666 <[email protected]> * cpp: WA (#896) Signed-off-by: LetMeFly666 <[email protected]> * cpp: WA (#896) Signed-off-by: LetMeFly666 <[email protected]> * cpp: AC (#896) Signed-off-by: LetMeFly666 <[email protected]> * cpp: AC (#896) Signed-off-by: LetMeFly666 <[email protected]> * init: Py (#896) Signed-off-by: LetMeFly666 <[email protected]> * AC: Py (#896) Signed-off-by: LetMeFly666 <[email protected]> * AC: Py (#896) Signed-off-by: LetMeFly666 <[email protected]> * init: Java (#896) Signed-off-by: LetMeFly666 <[email protected]> * CE: Java (#896) Signed-off-by: LetMeFly666 <[email protected]> * CE: Java (#896) Signed-off-by: LetMeFly666 <[email protected]> * WA: Java (#896) Signed-off-by: LetMeFly666 <[email protected]> * AC: Java (#896) Signed-off-by: LetMeFly666 <[email protected]> * AC: Java (#896) Signed-off-by: LetMeFly666 <[email protected]> * init: Go (#896) Signed-off-by: LetMeFly666 <[email protected]> * AC: Go (#896) Signed-off-by: LetMeFly666 <[email protected]> * format: Go (#896) Signed-off-by: LetMeFly666 <[email protected]> * AC: Go (#896) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2799.统计完全子数组的数目”的代码和题解(#897) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 1bbfa4a commit bb6a6ea

8 files changed

+333
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-24 22:47:03
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-24 23:05:27
6+
* @Description: AC,36.08%,63.38%
7+
*/
8+
#if defined(_WIN32) || defined(__APPLE__)
9+
#include "_[1,2]toVector.h"
10+
#endif
11+
class Solution {
12+
public:
13+
int countCompleteSubarrays(vector<int>& nums) {
14+
unordered_set<int> visited;
15+
for (int t : nums) {
16+
visited.insert(t);
17+
}
18+
int allType = visited.size();
19+
unordered_map<int, int> times;
20+
int ans = 0;
21+
for (int l = 0, r = 0; r < nums.size(); r++) {
22+
times[nums[r]]++;
23+
while (times.size() == allType && times[nums[l]] > 1) {
24+
times[nums[l++]]--;
25+
}
26+
if (times.size() == allType) {
27+
ans += l + 1;
28+
}
29+
}
30+
return ans;
31+
}
32+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-24 22:47:30
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-24 23:23:21
6+
* @Description: AC,32.53%,40.96%
7+
*/
8+
package main
9+
10+
func countCompleteSubarrays(nums []int) (ans int) {
11+
visited := map[int]bool{}
12+
for _, t := range nums {
13+
visited[t] = true
14+
}
15+
allType := len(visited)
16+
times := map[int]int{}
17+
l := 0
18+
for _, t := range nums {
19+
times[t]++
20+
for len(times) == allType && times[nums[l]] > 1 {
21+
times[nums[l]]--
22+
l++
23+
}
24+
if len(times) == allType {
25+
ans += l + 1
26+
}
27+
}
28+
return
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-24 22:47:48
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-24 23:18:36
6+
* @Description: AC,65.83%,85.83%
7+
*/
8+
import java.util.Set;
9+
import java.util.Map;
10+
import java.util.HashSet;
11+
import java.util.HashMap;
12+
13+
class Solution {
14+
public int countCompleteSubarrays(int[] nums) {
15+
Set<Integer> se = new HashSet<>();
16+
for (int t : nums) {
17+
se.add(t);
18+
}
19+
int allType = se.size();
20+
Map<Integer, Integer> times = new HashMap<>();
21+
int ans = 0;
22+
int l = 0;
23+
for (int t : nums) {
24+
times.merge(t, 1, Integer::sum);
25+
while (times.size() == allType && times.get(nums[l]) > 1) {
26+
times.merge(nums[l++], -1, Integer::sum);
27+
}
28+
if (times.size() == allType) {
29+
ans += l + 1;
30+
}
31+
}
32+
return ans;
33+
}
34+
}
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-24 22:47:44
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-24 23:10:14
6+
Description: AC,60.65%,29.08%
7+
'''
8+
from typing import List
9+
from collections import defaultdict
10+
11+
class Solution:
12+
def countCompleteSubarrays(self, nums: List[int]) -> int:
13+
allType = len(set(nums))
14+
times = defaultdict(int)
15+
l = ans = 0
16+
for r in range(len(nums)):
17+
times[nums[r]] += 1
18+
while len(times) == allType and times[nums[l]] > 1:
19+
times[nums[l]] -= 1
20+
l += 1
21+
if len(times) == allType:
22+
ans += l + 1
23+
return ans

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-04-14 00:11:36
5+
* @LastEditTime: 2025-04-24 10:29:33
66
-->
77
# LetLeet Blog
88

@@ -844,6 +844,7 @@
844844
|2788.按分隔符拆分字符串|简单|<a href="https://leetcode.cn/problems/split-strings-by-separator/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/20/LeetCode%202788.%E6%8C%89%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%86%E5%88%86%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135724019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-strings-by-separator/solutions/2612832/letmefly-2788an-fen-ge-fu-chai-fen-zi-fu-s1dn/" target="_blank">LeetCode题解</a>|
845845
|2789.合并后数组中的最大元素|中等|<a href="https://leetcode.cn/problems/largest-element-in-an-array-after-merge-operations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/14/LeetCode%202789.%E5%90%88%E5%B9%B6%E5%90%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136698122" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-element-in-an-array-after-merge-operations/solutions/2685943/letmefly-2789he-bing-hou-shu-zu-zhong-de-jrx8/" target="_blank">LeetCode题解</a>|
846846
|2798.满足目标工作时长的员工数目|简单|<a href="https://leetcode.cn/problems/number-of-employees-who-met-the-target/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/30/LeetCode%202798.%E6%BB%A1%E8%B6%B3%E7%9B%AE%E6%A0%87%E5%B7%A5%E4%BD%9C%E6%97%B6%E9%95%BF%E7%9A%84%E5%91%98%E5%B7%A5%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138352241" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-employees-who-met-the-target/solutions/2762052/letmefly-2798man-zu-mu-biao-gong-zuo-shi-2sb8/" target="_blank">LeetCode题解</a>|
847+
|2799.统计完全子数组的数目|中等|<a href="https://leetcode.cn/problems/count-complete-subarrays-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/24/LeetCode%202799.%E7%BB%9F%E8%AE%A1%E5%AE%8C%E5%85%A8%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/147494871" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-complete-subarrays-in-an-array/solutions/3661273/letmefly-2799tong-ji-wan-quan-zi-shu-zu-376bx/" target="_blank">LeetCode题解</a>|
847848
|2806.取整购买后的账户余额|简单|<a href="https://leetcode.cn/problems/account-balance-after-rounded-purchase/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/12/LeetCode%202806.%E5%8F%96%E6%95%B4%E8%B4%AD%E4%B9%B0%E5%90%8E%E7%9A%84%E8%B4%A6%E6%88%B7%E4%BD%99%E9%A2%9D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139621619" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/account-balance-after-rounded-purchase/solutions/2808399/letmefly-2806qu-zheng-gou-mai-hou-de-zha-fwxf/" target="_blank">LeetCode题解</a>|
848849
|2807.在链表中插入最大公约数|中等|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/06/LeetCode%202807.%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%8F%92%E5%85%A5%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135424056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/2593002/letmefly-2807zai-lian-biao-zhong-cha-ru-idjs4/" target="_blank">LeetCode题解</a>|
849850
|2808.使循环数组所有元素相等的最少秒数|中等|<a href="https://leetcode.cn/problems/minimum-seconds-to-equalize-a-circular-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/30/LeetCode%202808.%E4%BD%BF%E5%BE%AA%E7%8E%AF%E6%95%B0%E7%BB%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E7%9B%B8%E7%AD%89%E7%9A%84%E6%9C%80%E5%B0%91%E7%A7%92%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135930013" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-seconds-to-equalize-a-circular-array/solutions/2624998/letmefly-2808shi-xun-huan-shu-zu-suo-you-qm99/" target="_blank">LeetCode题解</a>|
@@ -1039,6 +1040,7 @@
10391040
- [ ] arknights主题不支持mermaid的渲染
10401041
- [ ] 研究DQT的[代码](https://github.com/LetMeFly666/LeetCode/tree/f14c448bc54f4efc3fa41b1d691d5e58a629353f/Codes/1366-rank-teams-by-votes_DQT-RE_version.cpp)为何RE
10411042
- [ ] Hexo的`$1\_2$`会被直接渲染成`$1_2$`,然后前端mathjs就会将$1\_2$解析成$1_2$。如[This](https://github.com/LetMeFly666/LeetCode/blob/7a007400b54908796c58576cb587c5f0a99550b8/Solutions/Other-Notes-Mianjing.md?plain=1#L358)。
1043+
- [ ] 总结贝叶斯公式 [1 简单推导](https://blog.csdn.net/weixin_41938903/article/details/105566524)、[2 条件概率公式原理](https://blog.csdn.net/u013066730/article/details/115249553)
10421044
- hexo我是一刻也待不下去了
10431045
- [x] 生成题解的时候还是按一下回车再开始吧,要不然想要像[这次](https://github.com/LetMeFly666/LeetCode/issues/787)多次边coding边提交,生成题解文件过早还得手动复制代码过去。
10441046
- [x] 写新题解时,若master本地为最新而远端并非最新,采用squash方式更新时,则远端pr会将本地master的一些commit也压缩为一个,远端和本地就冲突了。(还是保持master远端实时最新吧[#790](https://github.com/LetMeFly666/LeetCode/issues/790))
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: 2799.统计完全子数组的数目:滑动窗口(哈希表)
3+
date: 2025-04-24 23:24:12
4+
tags: [题解, LeetCode, 中等, 数组, 哈希表, set, map, 滑动窗口]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2799.统计完全子数组的数目:滑动窗口(哈希表)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-complete-subarrays-in-an-array/](https://leetcode.cn/problems/count-complete-subarrays-in-an-array/)
11+
12+
<p>给你一个由 <strong>正</strong> 整数组成的数组 <code>nums</code> 。</p>
13+
14+
<p>如果数组中的某个子数组满足下述条件,则称之为 <strong>完全子数组</strong> :</p>
15+
16+
<ul>
17+
<li>子数组中 <strong>不同</strong> 元素的数目等于整个数组不同元素的数目。</li>
18+
</ul>
19+
20+
<p>返回数组中 <strong>完全子数组</strong> 的数目。</p>
21+
22+
<p><strong>子数组</strong> 是数组中的一个连续非空序列。</p>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre><strong>输入:</strong>nums = [1,3,1,2,2]
29+
<strong>输出:</strong>4
30+
<strong>解释:</strong>完全子数组有:[1,3,1,2]、[1,3,1,2,2]、[3,1,2] 和 [3,1,2,2] 。
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre><strong>输入:</strong>nums = [5,5,5,5]
36+
<strong>输出:</strong>10
37+
<strong>解释:</strong>数组仅由整数 5 组成,所以任意子数组都满足完全子数组的条件。子数组的总数为 10 。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
46+
<li><code>1 &lt;= nums[i] &lt;= 2000</code></li>
47+
</ul>
48+
49+
50+
51+
## 解题方法:滑动窗口
52+
53+
首先使用一个哈希表统计数组中出现了多少种的元素(记为`allType`)。
54+
55+
接着再使用一个哈希表,统计窗口中每个元素出现多少次。
56+
57+
数组中的元素依次加入窗口中,当`窗口中元素种类数为allType`并且`窗口中第一个元素出现次数不为1`时,左移窗口左指针。
58+
59+
这样,就保证了每次窗口右指针确定时,左指针指向位置为最后一个“窗口合法”的位置(或0)。
60+
61+
+ 时间复杂度$O(len(nums))$
62+
+ 空间复杂度$O(len(nums))$
63+
64+
### AC代码
65+
66+
#### C++
67+
68+
```cpp
69+
/*
70+
* @Author: LetMeFly
71+
* @Date: 2025-04-24 22:47:03
72+
* @LastEditors: LetMeFly.xyz
73+
* @LastEditTime: 2025-04-24 23:05:27
74+
* @Description: AC,36.08%,63.38%
75+
*/
76+
class Solution {
77+
public:
78+
int countCompleteSubarrays(vector<int>& nums) {
79+
unordered_set<int> visited;
80+
for (int t : nums) {
81+
visited.insert(t);
82+
}
83+
int allType = visited.size();
84+
unordered_map<int, int> times;
85+
int ans = 0;
86+
for (int l = 0, r = 0; r < nums.size(); r++) {
87+
times[nums[r]]++;
88+
while (times.size() == allType && times[nums[l]] > 1) {
89+
times[nums[l++]]--;
90+
}
91+
if (times.size() == allType) {
92+
ans += l + 1;
93+
}
94+
}
95+
return ans;
96+
}
97+
};
98+
99+
```
100+
101+
#### Python
102+
103+
```python
104+
'''
105+
Author: LetMeFly
106+
Date: 2025-04-24 22:47:44
107+
LastEditors: LetMeFly.xyz
108+
LastEditTime: 2025-04-24 23:10:14
109+
Description: AC,60.65%,29.08%
110+
'''
111+
from typing import List
112+
from collections import defaultdict
113+
114+
class Solution:
115+
def countCompleteSubarrays(self, nums: List[int]) -> int:
116+
allType = len(set(nums))
117+
times = defaultdict(int)
118+
l = ans = 0
119+
for r in range(len(nums)):
120+
times[nums[r]] += 1
121+
while len(times) == allType and times[nums[l]] > 1:
122+
times[nums[l]] -= 1
123+
l += 1
124+
if len(times) == allType:
125+
ans += l + 1
126+
return ans
127+
```
128+
129+
#### Java
130+
131+
```java
132+
/*
133+
* @Author: LetMeFly
134+
* @Date: 2025-04-24 22:47:48
135+
* @LastEditors: LetMeFly.xyz
136+
* @LastEditTime: 2025-04-24 23:18:36
137+
* @Description: AC,65.83%,85.83%
138+
*/
139+
import java.util.Set;
140+
import java.util.Map;
141+
import java.util.HashSet;
142+
import java.util.HashMap;
143+
144+
class Solution {
145+
public int countCompleteSubarrays(int[] nums) {
146+
Set<Integer> se = new HashSet<>();
147+
for (int t : nums) {
148+
se.add(t);
149+
}
150+
int allType = se.size();
151+
Map<Integer, Integer> times = new HashMap<>();
152+
int ans = 0;
153+
int l = 0;
154+
for (int t : nums) {
155+
times.merge(t, 1, Integer::sum);
156+
while (times.size() == allType && times.get(nums[l]) > 1) {
157+
times.merge(nums[l++], -1, Integer::sum);
158+
}
159+
if (times.size() == allType) {
160+
ans += l + 1;
161+
}
162+
}
163+
return ans;
164+
}
165+
}
166+
```
167+
168+
#### Go
169+
170+
```go
171+
/*
172+
* @Author: LetMeFly
173+
* @Date: 2025-04-24 22:47:30
174+
* @LastEditors: LetMeFly.xyz
175+
* @LastEditTime: 2025-04-24 23:23:21
176+
* @Description: AC,32.53%,40.96%
177+
*/
178+
package main
179+
180+
func countCompleteSubarrays(nums []int) (ans int) {
181+
visited := map[int]bool{}
182+
for _, t := range nums {
183+
visited[t] = true
184+
}
185+
allType := len(visited)
186+
times := map[int]int{}
187+
l := 0
188+
for _, t := range nums {
189+
times[t]++
190+
for len(times) == allType && times[nums[l]] > 1 {
191+
times[nums[l]]--
192+
l++
193+
}
194+
if len(times) == allType {
195+
ans += l + 1
196+
}
197+
}
198+
return
199+
}
200+
```
201+
202+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147494871)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/24/LeetCode%202799.%E7%BB%9F%E8%AE%A1%E5%AE%8C%E5%85%A8%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/)哦~
203+
>
204+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,9 +1106,15 @@ categories: [自用]
11061106
|||
11071107
|stammer|v. 口吃,结结巴巴地说<br>n. 口吃,结巴|
11081108
|||
1109+
|unjust|adj. 不公平的,不公正的,非正义的|
1110+
|handkerchief|n. 手帕,纸巾|
1111+
|clientele|n. 客户|
1112+
|||
11091113
|terrace|n. 露天平台,阳台,阶梯看台,梯田|
11101114
|||
11111115
|bottle-neck|n. 瓶颈,道路上的狭窄处|
1116+
|||
1117+
|perimeter|n. 周长,外缘,边缘|
11121118

11131119
<p class="wordCounts">单词收录总数</p>
11141120

newSolution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2025-04-17 20:24:00
5+
LastEditTime: 2025-04-24 10:48:42
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -71,7 +71,7 @@ def getPlatform():
7171
issueNum = int(line.split()[0])
7272
print(issueNum)
7373
if not issueNum:
74-
issueCreateResult = os.popen(f'gh issue create -t "Who can add 1 more problem of LeetCode {num}" -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{lastSHA}/newSolution.py) using GH on {getPlatform()} " -l "题解" -a "@me"').read()
74+
issueCreateResult = os.popen(f'gh issue create -t "{issueTitle}" -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{lastSHA}/newSolution.py) using GH on {getPlatform()} " -l "题解" -a "@me"').read()
7575
print(issueCreateResult)
7676
issueNum = int(issueCreateResult.split('\n')[0].split('/')[-1])
7777

0 commit comments

Comments
 (0)