Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d2d6bb

Browse files
feat: add solutions to lc problem: No.2670 (doocs#2289)
No.2670.Find the Distinct Difference Array
1 parent f2a06e1 commit 9d2d6bb

File tree

7 files changed

+73
-196
lines changed

7 files changed

+73
-196
lines changed

‎solution/2600-2699/2670.Find the Distinct Difference Array/README.md‎

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,30 @@
5353

5454
## 解法
5555

56-
### 方法一
56+
### 方法一:哈希表 + 预处理后缀
57+
58+
我们可以预处理出一个后缀数组 $suf,ドル其中 $suf[i]$ 表示后缀 $nums[i, ..., n - 1]$ 中不同元素的数目,在预处理过程中,我们使用一个哈希表 $s$ 来维护后缀中出现过的元素,这样我们就可以在 $O(1)$ 的时间内查询后缀中不同元素的数目。
59+
60+
预处理完后缀数组 $suf$ 后,我们清空哈希表 $s,ドル然后再次遍历数组 $nums,ドル用哈希表 $s$ 来维护前缀中出现过的元素,那么位置 $i$ 的答案就是 $s$ 中不同元素的数目减去 $suf[i + 1],ドル即 $|s| - suf[i + 1]$。
61+
62+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
5763

5864
<!-- tabs:start -->
5965

6066
```python
6167
class Solution:
6268
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
6369
n = len(nums)
70+
suf = [0] * (n + 1)
71+
s = set()
72+
for i in range(n - 1, -1, -1):
73+
s.add(nums[i])
74+
suf[i] = len(s)
75+
s.clear()
6476
ans = [0] * n
65-
for i in range(n):
66-
a = len(set(nums[: i + 1]))
67-
b = len(set(nums[i + 1 :]))
68-
ans[i] = a - b
77+
for i, x in enumerate(nums):
78+
s.add(x)
79+
ans[i] = len(s) - suf[i + 1]
6980
return ans
7081
```
7182

@@ -134,14 +145,14 @@ func distinctDifferenceArray(nums []int) []int {
134145
```ts
135146
function distinctDifferenceArray(nums: number[]): number[] {
136147
const n = nums.length;
137-
const suf: number[] = newArray(n + 1).fill(0);
148+
const suf: number[] = Array(n + 1).fill(0);
138149
const s: Set<number> = new Set();
139150
for (let i = n - 1; i >= 0; --i) {
140151
s.add(nums[i]);
141152
suf[i] = s.size;
142153
}
143154
s.clear();
144-
const ans: number[] = newArray(n);
155+
const ans: number[] = Array(n).fill(0);
145156
for (let i = 0; i < n; ++i) {
146157
s.add(nums[i]);
147158
ans[i] = s.size - suf[i + 1];
@@ -153,76 +164,22 @@ function distinctDifferenceArray(nums: number[]): number[] {
153164
```rust
154165
use std::collections::HashSet;
155166

156-
impl Solution {
157-
pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
158-
let mut ans: Vec<i32> = Vec::new();
159-
160-
for i in 0..nums.len() {
161-
let mut j = 0;
162-
let mut hash1 = HashSet::new();
163-
while j <= i {
164-
hash1.insert(nums[j]);
165-
j += 1;
166-
}
167-
168-
let mut k = i + 1;
169-
let mut hash2 = HashSet::new();
170-
while k < nums.len() {
171-
hash2.insert(nums[k]);
172-
k += 1;
173-
}
174-
175-
ans.push((hash1.len() - hash2.len()) as i32);
176-
}
177-
178-
ans
179-
}
180-
}
181-
```
182-
183-
<!-- tabs:end -->
184-
185-
### 方法二
186-
187-
<!-- tabs:start -->
188-
189-
```python
190-
class Solution:
191-
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
192-
n = len(nums)
193-
suf = [0] * (n + 1)
194-
s = set()
195-
for i in range(n - 1, -1, -1):
196-
s.add(nums[i])
197-
suf[i] = len(s)
198-
199-
s.clear()
200-
ans = [0] * n
201-
for i, x in enumerate(nums):
202-
s.add(x)
203-
ans[i] = len(s) - suf[i + 1]
204-
return ans
205-
```
206-
207-
```rust
208-
use std::collections::HashSet;
209-
210167
impl Solution {
211168
pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
212169
let n = nums.len();
213-
let mut s = vec![0; n + 1];
214-
let mut set = HashSet::new();
170+
let mut suf = vec![0; n + 1];
171+
let mut s = HashSet::new();
215172

216173
for i in (0..n).rev() {
217-
set.insert(nums[i]);
218-
s[i] = set.len();
174+
s.insert(nums[i]);
175+
suf[i] = s.len();
219176
}
220177

221178
let mut ans = Vec::new();
222-
set.clear();
179+
s.clear();
223180
for i in 0..n {
224-
set.insert(nums[i]);
225-
ans.push((set.len() - s[i + 1]) as i32);
181+
s.insert(nums[i]);
182+
ans.push((s.len() - suf[i + 1]) as i32);
226183
}
227184

228185
ans

‎solution/2600-2699/2670.Find the Distinct Difference Array/README_EN.md‎

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,30 @@ For index i = 4, there are 3 distinct elements in the prefix and no elements in
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Hash Table + Preprocessed Suffix
51+
52+
We can preprocess a suffix array $suf,ドル where $suf[i]$ represents the number of distinct elements in the suffix $nums[i, ..., n - 1]$. During the preprocessing, we use a hash table $s$ to maintain the elements that have appeared in the suffix, so we can query the number of distinct elements in the suffix in $O(1)$ time.
53+
54+
After preprocessing the suffix array $suf,ドル we clear the hash table $s,ドル and then traverse the array $nums$ again, using the hash table $s$ to maintain the elements that have appeared in the prefix. The answer at position $i$ is the number of distinct elements in $s$ minus $suf[i + 1],ドル that is, $s.size() - suf[i + 1]$.
55+
56+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
5157

5258
<!-- tabs:start -->
5359

5460
```python
5561
class Solution:
5662
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
5763
n = len(nums)
64+
suf = [0] * (n + 1)
65+
s = set()
66+
for i in range(n - 1, -1, -1):
67+
s.add(nums[i])
68+
suf[i] = len(s)
69+
s.clear()
5870
ans = [0] * n
59-
for i in range(n):
60-
a = len(set(nums[: i + 1]))
61-
b = len(set(nums[i + 1 :]))
62-
ans[i] = a - b
71+
for i, x in enumerate(nums):
72+
s.add(x)
73+
ans[i] = len(s) - suf[i + 1]
6374
return ans
6475
```
6576

@@ -128,14 +139,14 @@ func distinctDifferenceArray(nums []int) []int {
128139
```ts
129140
function distinctDifferenceArray(nums: number[]): number[] {
130141
const n = nums.length;
131-
const suf: number[] = newArray(n + 1).fill(0);
142+
const suf: number[] = Array(n + 1).fill(0);
132143
const s: Set<number> = new Set();
133144
for (let i = n - 1; i >= 0; --i) {
134145
s.add(nums[i]);
135146
suf[i] = s.size;
136147
}
137148
s.clear();
138-
const ans: number[] = newArray(n);
149+
const ans: number[] = Array(n).fill(0);
139150
for (let i = 0; i < n; ++i) {
140151
s.add(nums[i]);
141152
ans[i] = s.size - suf[i + 1];
@@ -147,76 +158,22 @@ function distinctDifferenceArray(nums: number[]): number[] {
147158
```rust
148159
use std::collections::HashSet;
149160

150-
impl Solution {
151-
pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
152-
let mut ans: Vec<i32> = Vec::new();
153-
154-
for i in 0..nums.len() {
155-
let mut j = 0;
156-
let mut hash1 = HashSet::new();
157-
while j <= i {
158-
hash1.insert(nums[j]);
159-
j += 1;
160-
}
161-
162-
let mut k = i + 1;
163-
let mut hash2 = HashSet::new();
164-
while k < nums.len() {
165-
hash2.insert(nums[k]);
166-
k += 1;
167-
}
168-
169-
ans.push((hash1.len() - hash2.len()) as i32);
170-
}
171-
172-
ans
173-
}
174-
}
175-
```
176-
177-
<!-- tabs:end -->
178-
179-
### Solution 2
180-
181-
<!-- tabs:start -->
182-
183-
```python
184-
class Solution:
185-
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
186-
n = len(nums)
187-
suf = [0] * (n + 1)
188-
s = set()
189-
for i in range(n - 1, -1, -1):
190-
s.add(nums[i])
191-
suf[i] = len(s)
192-
193-
s.clear()
194-
ans = [0] * n
195-
for i, x in enumerate(nums):
196-
s.add(x)
197-
ans[i] = len(s) - suf[i + 1]
198-
return ans
199-
```
200-
201-
```rust
202-
use std::collections::HashSet;
203-
204161
impl Solution {
205162
pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
206163
let n = nums.len();
207-
let mut s = vec![0; n + 1];
208-
let mut set = HashSet::new();
164+
let mut suf = vec![0; n + 1];
165+
let mut s = HashSet::new();
209166

210167
for i in (0..n).rev() {
211-
set.insert(nums[i]);
212-
s[i] = set.len();
168+
s.insert(nums[i]);
169+
suf[i] = s.len();
213170
}
214171

215172
let mut ans = Vec::new();
216-
set.clear();
173+
s.clear();
217174
for i in 0..n {
218-
set.insert(nums[i]);
219-
ans.push((set.len() - s[i + 1]) as i32);
175+
s.insert(nums[i]);
176+
ans.push((s.len() - suf[i + 1]) as i32);
220177
}
221178

222179
ans
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class Solution:
22
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
33
n = len(nums)
4+
suf = [0] * (n + 1)
5+
s = set()
6+
for i in range(n - 1, -1, -1):
7+
s.add(nums[i])
8+
suf[i] = len(s)
9+
s.clear()
410
ans = [0] * n
5-
for i in range(n):
6-
a = len(set(nums[: i + 1]))
7-
b = len(set(nums[i + 1 :]))
8-
ans[i] = a - b
11+
for i, x in enumerate(nums):
12+
s.add(x)
13+
ans[i] = len(s) - suf[i + 1]
914
return ans

‎solution/2600-2699/2670.Find the Distinct Difference Array/Solution.rs‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ use std::collections::HashSet;
22

33
impl Solution {
44
pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
5-
let mut ans: Vec<i32> = Vec::new();
5+
let n = nums.len();
6+
let mut suf = vec![0; n + 1];
7+
let mut s = HashSet::new();
68

7-
for i in 0..nums.len() {
8-
let mut j = 0;
9-
let mut hash1 = HashSet::new();
10-
while j <= i {
11-
hash1.insert(nums[j]);
12-
j += 1;
13-
}
14-
15-
let mut k = i + 1;
16-
let mut hash2 = HashSet::new();
17-
while k < nums.len() {
18-
hash2.insert(nums[k]);
19-
k += 1;
20-
}
9+
for i in (0..n).rev() {
10+
s.insert(nums[i]);
11+
suf[i] = s.len();
12+
}
2113

22-
ans.push((hash1.len() - hash2.len()) as i32);
14+
let mut ans = Vec::new();
15+
s.clear();
16+
for i in 0..n {
17+
s.insert(nums[i]);
18+
ans.push((s.len() - suf[i + 1]) as i32);
2319
}
2420

2521
ans

‎solution/2600-2699/2670.Find the Distinct Difference Array/Solution.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
function distinctDifferenceArray(nums: number[]): number[] {
22
const n = nums.length;
3-
const suf: number[] = newArray(n + 1).fill(0);
3+
const suf: number[] = Array(n + 1).fill(0);
44
const s: Set<number> = new Set();
55
for (let i = n - 1; i >= 0; --i) {
66
s.add(nums[i]);
77
suf[i] = s.size;
88
}
99
s.clear();
10-
const ans: number[] = newArray(n);
10+
const ans: number[] = Array(n).fill(0);
1111
for (let i = 0; i < n; ++i) {
1212
s.add(nums[i]);
1313
ans[i] = s.size - suf[i + 1];

‎solution/2600-2699/2670.Find the Distinct Difference Array/Solution2.py‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎solution/2600-2699/2670.Find the Distinct Difference Array/Solution2.rs‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /