Skip to content

Commit a71e487

Browse files
committed
feat: add solutions to lc problem: No.1852
No.1852.Distinct Numbers in Each Subarray
1 parent 5933a84 commit a71e487

File tree

6 files changed

+264
-5
lines changed

6 files changed

+264
-5
lines changed

solution/1800-1899/1852.Distinct Numbers in Each Subarray/README.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,116 @@
5050

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

53+
**方法一:滑动窗口 + 哈希表/数组**
54+
55+
用数组或哈希表记录每个窗口大小为 $k$ 的子数组中的数字出现的次数,然后遍历数组,每次更新哈希表,并记录当前窗口中数字的种类数。
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是数组的长度。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
5662

5763
<!-- 这里可写当前语言的特殊实现逻辑 -->
5864

5965
```python
60-
66+
class Solution:
67+
def distinctNumbers(self, nums: List[int], k: int) -> List[int]:
68+
n = len(nums)
69+
cnt = Counter(nums[:k])
70+
ans = [len(cnt)]
71+
for i in range(k, n):
72+
u = nums[i - k]
73+
cnt[u] -= 1
74+
if cnt[u] == 0:
75+
cnt.pop(u)
76+
77+
cnt[nums[i]] += 1
78+
ans.append(len(cnt))
79+
return ans
6180
```
6281

6382
### **Java**
6483

6584
<!-- 这里可写当前语言的特殊实现逻辑 -->
6685

6786
```java
87+
class Solution {
88+
public int[] distinctNumbers(int[] nums, int k) {
89+
int[] cnt = new int[100010];
90+
int x = 0;
91+
for (int i = 0; i < k; ++i) {
92+
if (cnt[nums[i]]++ == 0) {
93+
++x;
94+
}
95+
}
96+
int n = nums.length;
97+
int[] ans = new int[n - k + 1];
98+
ans[0] = x;
99+
for (int i = k; i < n; ++i) {
100+
if (--cnt[nums[i - k]] == 0) {
101+
--x;
102+
}
103+
if (cnt[nums[i]]++ == 0) {
104+
++x;
105+
}
106+
ans[i - k + 1] = x;
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
vector<int> distinctNumbers(vector<int>& nums, int k) {
119+
int cnt[100010] = {0};
120+
int x = 0;
121+
for (int i = 0; i < k; ++i) {
122+
if (cnt[nums[i]]++ == 0) {
123+
++x;
124+
}
125+
}
126+
int n = nums.size();
127+
vector<int> ans(n - k + 1);
128+
ans[0] = x;
129+
for (int i = k; i < n; ++i) {
130+
if (--cnt[nums[i - k]] == 0) {
131+
--x;
132+
}
133+
if (cnt[nums[i]]++ == 0) {
134+
++x;
135+
}
136+
ans[i - k + 1] = x;
137+
}
138+
return ans;
139+
}
140+
};
141+
```
68142
143+
### **Go**
144+
145+
```go
146+
func distinctNumbers(nums []int, k int) []int {
147+
cnt := map[int]int{}
148+
for _, v := range nums[:k] {
149+
cnt[v]++
150+
}
151+
ans := []int{len(cnt)}
152+
for i := k; i < len(nums); i++ {
153+
u := nums[i-k]
154+
cnt[u]--
155+
if cnt[u] == 0 {
156+
delete(cnt, u)
157+
}
158+
cnt[nums[i]]++
159+
ans = append(ans, len(cnt))
160+
}
161+
return ans
162+
}
69163
```
70164

71165
### **...**

solution/1800-1899/1852.Distinct Numbers in Each Subarray/README_EN.md

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@
5757
<p><strong>Constraints:</strong></p>
5858

5959
<ul>
60-
6160
<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
62-
6361
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
64-
6562
</ul>
6663

6764
## Solutions
@@ -71,13 +68,101 @@
7168
### **Python3**
7269

7370
```python
74-
71+
class Solution:
72+
def distinctNumbers(self, nums: List[int], k: int) -> List[int]:
73+
n = len(nums)
74+
cnt = Counter(nums[:k])
75+
ans = [len(cnt)]
76+
for i in range(k, n):
77+
u = nums[i - k]
78+
cnt[u] -= 1
79+
if cnt[u] == 0:
80+
cnt.pop(u)
81+
82+
cnt[nums[i]] += 1
83+
ans.append(len(cnt))
84+
return ans
7585
```
7686

7787
### **Java**
7888

7989
```java
90+
class Solution {
91+
public int[] distinctNumbers(int[] nums, int k) {
92+
int[] cnt = new int[100010];
93+
int x = 0;
94+
for (int i = 0; i < k; ++i) {
95+
if (cnt[nums[i]]++ == 0) {
96+
++x;
97+
}
98+
}
99+
int n = nums.length;
100+
int[] ans = new int[n - k + 1];
101+
ans[0] = x;
102+
for (int i = k; i < n; ++i) {
103+
if (--cnt[nums[i - k]] == 0) {
104+
--x;
105+
}
106+
if (cnt[nums[i]]++ == 0) {
107+
++x;
108+
}
109+
ans[i - k + 1] = x;
110+
}
111+
return ans;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
vector<int> distinctNumbers(vector<int>& nums, int k) {
122+
int cnt[100010] = {0};
123+
int x = 0;
124+
for (int i = 0; i < k; ++i) {
125+
if (cnt[nums[i]]++ == 0) {
126+
++x;
127+
}
128+
}
129+
int n = nums.size();
130+
vector<int> ans(n - k + 1);
131+
ans[0] = x;
132+
for (int i = k; i < n; ++i) {
133+
if (--cnt[nums[i - k]] == 0) {
134+
--x;
135+
}
136+
if (cnt[nums[i]]++ == 0) {
137+
++x;
138+
}
139+
ans[i - k + 1] = x;
140+
}
141+
return ans;
142+
}
143+
};
144+
```
80145
146+
### **Go**
147+
148+
```go
149+
func distinctNumbers(nums []int, k int) []int {
150+
cnt := map[int]int{}
151+
for _, v := range nums[:k] {
152+
cnt[v]++
153+
}
154+
ans := []int{len(cnt)}
155+
for i := k; i < len(nums); i++ {
156+
u := nums[i-k]
157+
cnt[u]--
158+
if cnt[u] == 0 {
159+
delete(cnt, u)
160+
}
161+
cnt[nums[i]]++
162+
ans = append(ans, len(cnt))
163+
}
164+
return ans
165+
}
81166
```
82167

83168
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<int> distinctNumbers(vector<int>& nums, int k) {
4+
int cnt[100010] = {0};
5+
int x = 0;
6+
for (int i = 0; i < k; ++i) {
7+
if (cnt[nums[i]]++ == 0) {
8+
++x;
9+
}
10+
}
11+
int n = nums.size();
12+
vector<int> ans(n - k + 1);
13+
ans[0] = x;
14+
for (int i = k; i < n; ++i) {
15+
if (--cnt[nums[i - k]] == 0) {
16+
--x;
17+
}
18+
if (cnt[nums[i]]++ == 0) {
19+
++x;
20+
}
21+
ans[i - k + 1] = x;
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func distinctNumbers(nums []int, k int) []int {
2+
cnt := map[int]int{}
3+
for _, v := range nums[:k] {
4+
cnt[v]++
5+
}
6+
ans := []int{len(cnt)}
7+
for i := k; i < len(nums); i++ {
8+
u := nums[i-k]
9+
cnt[u]--
10+
if cnt[u] == 0 {
11+
delete(cnt, u)
12+
}
13+
cnt[nums[i]]++
14+
ans = append(ans, len(cnt))
15+
}
16+
return ans
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] distinctNumbers(int[] nums, int k) {
3+
int[] cnt = new int[100010];
4+
int x = 0;
5+
for (int i = 0; i < k; ++i) {
6+
if (cnt[nums[i]]++ == 0) {
7+
++x;
8+
}
9+
}
10+
int n = nums.length;
11+
int[] ans = new int[n - k + 1];
12+
ans[0] = x;
13+
for (int i = k; i < n; ++i) {
14+
if (--cnt[nums[i - k]] == 0) {
15+
--x;
16+
}
17+
if (cnt[nums[i]]++ == 0) {
18+
++x;
19+
}
20+
ans[i - k + 1] = x;
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def distinctNumbers(self, nums: List[int], k: int) -> List[int]:
3+
n = len(nums)
4+
cnt = Counter(nums[:k])
5+
ans = [len(cnt)]
6+
for i in range(k, n):
7+
u = nums[i - k]
8+
cnt[u] -= 1
9+
if cnt[u] == 0:
10+
cnt.pop(u)
11+
12+
cnt[nums[i]] += 1
13+
ans.append(len(cnt))
14+
return ans

0 commit comments

Comments
 (0)