Skip to content

Commit 2bae413

Browse files
committed
feat: add solutions to lc problem: no.2465
No.2465.Number of Distinct Averages
1 parent 5d24fbc commit 2bae413

File tree

7 files changed

+231
-30
lines changed

7 files changed

+231
-30
lines changed

Diff for: solution/2400-2499/2465.Number of Distinct Averages/README.md

+99-10
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@
6161

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

64-
**方法一:排序 + 哈希表**
64+
**方法一:排序**
6565

66-
我们先对数组进行排序,然后遍历数组,将 $nums[i]$ 与 $nums[n-i-1]$ 求和后放入哈希表中,最后返回哈希表的大小即可
66+
题目中要求每次找到数组 $nums$ 中的最小值和最大值,然后删除它们,再计算删除两数的平均值。因此,我们可以先对数组 $nums$ 进行排序,然后每次取数组的首尾元素,计算它们的和,用哈希表或数组 $cnt$ 记录每个和出现的次数,最后统计不同的和的个数即可
6767

68-
时间复杂度 $O(n\times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度
68+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度
6969

7070
<!-- tabs:start -->
7171

@@ -76,9 +76,22 @@
7676
```python
7777
class Solution:
7878
def distinctAverages(self, nums: List[int]) -> int:
79-
n = len(nums)
8079
nums.sort()
81-
return len(set(nums[i] + nums[n - i - 1] for i in range(n >> 1)))
80+
return len(set(nums[i] + nums[-i - 1] for i in range(len(nums) >> 1)))
81+
```
82+
83+
```python
84+
class Solution:
85+
def distinctAverages(self, nums: List[int]) -> int:
86+
nums.sort()
87+
ans = 0
88+
cnt = Counter()
89+
for i in range(len(nums) >> 1):
90+
x = nums[i] + nums[-i - 1]
91+
cnt[x] += 1
92+
if cnt[x] == 1:
93+
ans += 1
94+
return ans
8295
```
8396

8497
### **Java**
@@ -89,35 +102,72 @@ class Solution:
89102
class Solution {
90103
public int distinctAverages(int[] nums) {
91104
Arrays.sort(nums);
92-
int n = nums.length;
93105
Set<Integer> s = new HashSet<>();
94-
for (int i = 0; i<n> > 1; ++i) {
106+
int n = nums.length;
107+
for (int i = 0; i < n >> 1; ++i) {
95108
s.add(nums[i] + nums[n - i - 1]);
96109
}
97110
return s.size();
98111
}
99112
}
100113
```
101114

115+
```java
116+
class Solution {
117+
public int distinctAverages(int[] nums) {
118+
Arrays.sort(nums);
119+
int[] cnt = new int[201];
120+
int n = nums.length;
121+
int ans = 0;
122+
for (int i = 0; i < n >> 1; ++i) {
123+
if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
124+
++ans;
125+
}
126+
}
127+
return ans;
128+
}
129+
}
130+
```
131+
102132
### **C++**
103133

104134
```cpp
105135
class Solution {
106136
public:
107137
int distinctAverages(vector<int>& nums) {
108138
sort(nums.begin(), nums.end());
109-
int n = nums.size();
110139
unordered_set<int> s;
111-
for (int i = 0; i < n >> 1; ++i) s.insert(nums[i] + nums[n - i - 1]);
140+
int n = nums.size();
141+
for (int i = 0; i < n >> 1; ++i) {
142+
s.insert(nums[i] + nums[n - i - 1]);
143+
}
112144
return s.size();
113145
}
114146
};
115147
```
116148
149+
```cpp
150+
class Solution {
151+
public:
152+
int distinctAverages(vector<int>& nums) {
153+
sort(nums.begin(), nums.end());
154+
int cnt[201]{};
155+
int n = nums.size();
156+
int ans = 0;
157+
for (int i = 0; i < n >> 1; ++i) {
158+
if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
159+
++ans;
160+
}
161+
}
162+
return ans;
163+
}
164+
};
165+
```
166+
117167
### **Go**
118168

119169
```go
120-
func distinctAverages(nums []int) int {
170+
func distinctAverages(nums []int) (ans int) {
121171
sort.Ints(nums)
122172
n := len(nums)
123173
s := map[int]struct{}{}
@@ -128,10 +178,49 @@ func distinctAverages(nums []int) int {
128178
}
129179
```
130180

181+
```go
182+
func distinctAverages(nums []int) (ans int) {
183+
sort.Ints(nums)
184+
n := len(nums)
185+
cnt := [201]int{}
186+
for i := 0; i < n>>1; i++ {
187+
x := nums[i] + nums[n-i-1]
188+
cnt[x]++
189+
if cnt[x] == 1 {
190+
ans++
191+
}
192+
}
193+
return
194+
}
195+
```
196+
131197
### **TypeScript**
132198

133199
```ts
200+
function distinctAverages(nums: number[]): number {
201+
nums.sort((a, b) => a - b);
202+
const s: Set<number> = new Set();
203+
const n = nums.length;
204+
for (let i = 0; i < n >> 1; ++i) {
205+
s.add(nums[i] + nums[n - i - 1]);
206+
}
207+
return s.size;
208+
}
209+
```
134210

211+
```ts
212+
function distinctAverages(nums: number[]): number {
213+
nums.sort((a, b) => a - b);
214+
const cnt: number[] = Array(201).fill(0);
215+
let ans = 0;
216+
const n = nums.length;
217+
for (let i = 0; i < n >> 1; ++i) {
218+
if (++cnt[nums[i] + nums[n - i - 1]] === 1) {
219+
++ans;
220+
}
221+
}
222+
return ans;
223+
}
135224
```
136225

137226
### **...**

Diff for: solution/2400-2499/2465.Number of Distinct Averages/README_EN.md

+96-7
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,22 @@ There is only one average to be calculated after removing 1 and 100, so we retur
6464
```python
6565
class Solution:
6666
def distinctAverages(self, nums: List[int]) -> int:
67-
n = len(nums)
6867
nums.sort()
69-
return len(set(nums[i] + nums[n - i - 1] for i in range(n >> 1)))
68+
return len(set(nums[i] + nums[-i - 1] for i in range(len(nums) >> 1)))
69+
```
70+
71+
```python
72+
class Solution:
73+
def distinctAverages(self, nums: List[int]) -> int:
74+
nums.sort()
75+
ans = 0
76+
cnt = Counter()
77+
for i in range(len(nums) >> 1):
78+
x = nums[i] + nums[-i - 1]
79+
cnt[x] += 1
80+
if cnt[x] == 1:
81+
ans += 1
82+
return ans
7083
```
7184

7285
### **Java**
@@ -75,35 +88,72 @@ class Solution:
7588
class Solution {
7689
public int distinctAverages(int[] nums) {
7790
Arrays.sort(nums);
78-
int n = nums.length;
7991
Set<Integer> s = new HashSet<>();
80-
for (int i = 0; i<n> > 1; ++i) {
92+
int n = nums.length;
93+
for (int i = 0; i < n >> 1; ++i) {
8194
s.add(nums[i] + nums[n - i - 1]);
8295
}
8396
return s.size();
8497
}
8598
}
8699
```
87100

101+
```java
102+
class Solution {
103+
public int distinctAverages(int[] nums) {
104+
Arrays.sort(nums);
105+
int[] cnt = new int[201];
106+
int n = nums.length;
107+
int ans = 0;
108+
for (int i = 0; i < n >> 1; ++i) {
109+
if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
110+
++ans;
111+
}
112+
}
113+
return ans;
114+
}
115+
}
116+
```
117+
88118
### **C++**
89119

90120
```cpp
91121
class Solution {
92122
public:
93123
int distinctAverages(vector<int>& nums) {
94124
sort(nums.begin(), nums.end());
95-
int n = nums.size();
96125
unordered_set<int> s;
97-
for (int i = 0; i < n >> 1; ++i) s.insert(nums[i] + nums[n - i - 1]);
126+
int n = nums.size();
127+
for (int i = 0; i < n >> 1; ++i) {
128+
s.insert(nums[i] + nums[n - i - 1]);
129+
}
98130
return s.size();
99131
}
100132
};
101133
```
102134
135+
```cpp
136+
class Solution {
137+
public:
138+
int distinctAverages(vector<int>& nums) {
139+
sort(nums.begin(), nums.end());
140+
int cnt[201]{};
141+
int n = nums.size();
142+
int ans = 0;
143+
for (int i = 0; i < n >> 1; ++i) {
144+
if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
145+
++ans;
146+
}
147+
}
148+
return ans;
149+
}
150+
};
151+
```
152+
103153
### **Go**
104154

105155
```go
106-
func distinctAverages(nums []int) int {
156+
func distinctAverages(nums []int) (ans int) {
107157
sort.Ints(nums)
108158
n := len(nums)
109159
s := map[int]struct{}{}
@@ -114,10 +164,49 @@ func distinctAverages(nums []int) int {
114164
}
115165
```
116166

167+
```go
168+
func distinctAverages(nums []int) (ans int) {
169+
sort.Ints(nums)
170+
n := len(nums)
171+
cnt := [201]int{}
172+
for i := 0; i < n>>1; i++ {
173+
x := nums[i] + nums[n-i-1]
174+
cnt[x]++
175+
if cnt[x] == 1 {
176+
ans++
177+
}
178+
}
179+
return
180+
}
181+
```
182+
117183
### **TypeScript**
118184

119185
```ts
186+
function distinctAverages(nums: number[]): number {
187+
nums.sort((a, b) => a - b);
188+
const s: Set<number> = new Set();
189+
const n = nums.length;
190+
for (let i = 0; i < n >> 1; ++i) {
191+
s.add(nums[i] + nums[n - i - 1]);
192+
}
193+
return s.size;
194+
}
195+
```
120196

197+
```ts
198+
function distinctAverages(nums: number[]): number {
199+
nums.sort((a, b) => a - b);
200+
const cnt: number[] = Array(201).fill(0);
201+
let ans = 0;
202+
const n = nums.length;
203+
for (let i = 0; i < n >> 1; ++i) {
204+
if (++cnt[nums[i] + nums[n - i - 1]] === 1) {
205+
++ans;
206+
}
207+
}
208+
return ans;
209+
}
121210
```
122211

123212
### **...**

Diff for: solution/2400-2499/2465.Number of Distinct Averages/Solution.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ class Solution {
22
public:
33
int distinctAverages(vector<int>& nums) {
44
sort(nums.begin(), nums.end());
5+
int cnt[201]{};
56
int n = nums.size();
6-
unordered_set<int> s;
7-
for (int i = 0; i<n> > 1; ++i) s.insert(nums[i] + nums[n - i - 1]);
8-
return s.size();
7+
int ans = 0;
8+
for (int i = 0; i < n >> 1; ++i) {
9+
if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
10+
++ans;
11+
}
12+
}
13+
return ans;
914
}
1015
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
func distinctAverages(nums []int) int {
1+
func distinctAverages(nums []int) (ans int) {
22
sort.Ints(nums)
33
n := len(nums)
4-
s := map[int]struct{}{}
4+
cnt := [201]int{}
55
for i := 0; i < n>>1; i++ {
6-
s[nums[i]+nums[n-i-1]] = struct{}{}
6+
x := nums[i] + nums[n-i-1]
7+
cnt[x]++
8+
if cnt[x] == 1 {
9+
ans++
10+
}
711
}
8-
return len(s)
12+
return
913
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
class Solution {
22
public int distinctAverages(int[] nums) {
33
Arrays.sort(nums);
4+
int[] cnt = new int[201];
45
int n = nums.length;
5-
Set<Integer> s = new HashSet<>();
6-
for (int i = 0; i<n> > 1; ++i) {
7-
s.add(nums[i] + nums[n - i - 1]);
6+
int ans = 0;
7+
for (int i = 0; i < n >> 1; ++i) {
8+
if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
9+
++ans;
10+
}
811
}
9-
return s.size();
12+
return ans;
1013
}
1114
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution:
22
def distinctAverages(self, nums: List[int]) -> int:
3-
n = len(nums)
43
nums.sort()
5-
return len(set(nums[i] + nums[n - i - 1] for i in range(n >> 1)))
4+
return len(set(nums[i] + nums[-i - 1] for i in range(len(nums) >> 1)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function distinctAverages(nums: number[]): number {
2+
nums.sort((a, b) => a - b);
3+
const cnt: number[] = Array(201).fill(0);
4+
let ans = 0;
5+
const n = nums.length;
6+
for (let i = 0; i < n >> 1; ++i) {
7+
if (++cnt[nums[i] + nums[n - i - 1]] === 1) {
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)