Skip to content

Commit 3606612

Browse files
committed
feat: add solutions to lc problem: No.3678
No.3678.Smallest Absent Positive Greater Than Average
1 parent 58634a4 commit 3606612

File tree

7 files changed

+210
-8
lines changed

7 files changed

+210
-8
lines changed

solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README.md

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,103 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3678.Sm
8181

8282
<!-- solution:start -->
8383

84-
### 方法一
84+
### 方法一:哈希表
85+
86+
我们用一个哈希表 $\textit{s}$ 来记录数组 $\textit{nums}$ 中出现过的元素。
87+
88+
然后,我们计算数组 $\textit{nums}$ 的平均值 $\textit{avg}$,并将答案 $\textit{ans}$ 初始化为 $\max(1, \lfloor \textit{avg} \rfloor + 1)$。
89+
90+
如果 $\textit{ans}$ 在 $\textit{s}$ 中出现过,那么我们将 $\textit{ans}$ 加一,直到 $\textit{ans}$ 不在 $\textit{s}$ 中出现过为止。
91+
92+
最后,返回 $\textit{ans}$ 即可。
93+
94+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。
8595

8696
<!-- tabs:start -->
8797

8898
#### Python3
8999

90100
```python
91-
101+
class Solution:
102+
def smallestAbsent(self, nums: List[int]) -> int:
103+
s = set(nums)
104+
ans = max(1, sum(nums) // len(nums) + 1)
105+
while ans in s:
106+
ans += 1
107+
return ans
92108
```
93109

94110
#### Java
95111

96112
```java
97-
113+
class Solution {
114+
public int smallestAbsent(int[] nums) {
115+
Set<Integer> s = new HashSet<>();
116+
int sum = 0;
117+
for (int x : nums) {
118+
s.add(x);
119+
sum += x;
120+
}
121+
int ans = Math.max(1, sum / nums.length + 1);
122+
while (s.contains(ans)) {
123+
++ans;
124+
}
125+
return ans;
126+
}
127+
}
98128
```
99129

100130
#### C++
101131

102132
```cpp
103-
133+
class Solution {
134+
public:
135+
int smallestAbsent(vector<int>& nums) {
136+
unordered_set<int> s;
137+
int sum = 0;
138+
for (int x : nums) {
139+
s.insert(x);
140+
sum += x;
141+
}
142+
int ans = max(1, sum / (int) nums.size() + 1);
143+
while (s.contains(ans)) {
144+
++ans;
145+
}
146+
return ans;
147+
}
148+
};
104149
```
105150
106151
#### Go
107152
108153
```go
154+
func smallestAbsent(nums []int) int {
155+
s := map[int]bool{}
156+
sum := 0
157+
for _, x := range nums {
158+
s[x] = true
159+
sum += x
160+
}
161+
ans := max(1, sum/len(nums)+1)
162+
for s[ans] {
163+
ans++
164+
}
165+
return ans
166+
}
167+
```
109168

169+
#### TypeScript
170+
171+
```ts
172+
function smallestAbsent(nums: number[]): number {
173+
const s = new Set<number>(nums);
174+
const sum = nums.reduce((a, b) => a + b, 0);
175+
let ans = Math.max(1, Math.floor(sum / nums.length) + 1);
176+
while (s.has(ans)) {
177+
ans++;
178+
}
179+
return ans;
180+
}
110181
```
111182

112183
<!-- tabs:end -->

solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README_EN.md

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,103 @@ The <strong>average</strong> of an array is defined as the sum of all its elemen
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Hash Map
82+
83+
We use a hash map $\textit{s}$ to record the elements that appear in the array $\textit{nums}$.
84+
85+
Then, we calculate the average value $\textit{avg}$ of the array $\textit{nums}$, and initialize the answer $\textit{ans}$ as $\max(1, \lfloor \textit{avg} \rfloor + 1)$.
86+
87+
If $\textit{ans}$ appears in $\textit{s}$, we increment $\textit{ans}$ until it no longer appears in $\textit{s}$.
88+
89+
Finally, we return $\textit{ans}$.
90+
91+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
8292

8393
<!-- tabs:start -->
8494

8595
#### Python3
8696

8797
```python
88-
98+
class Solution:
99+
def smallestAbsent(self, nums: List[int]) -> int:
100+
s = set(nums)
101+
ans = max(1, sum(nums) // len(nums) + 1)
102+
while ans in s:
103+
ans += 1
104+
return ans
89105
```
90106

91107
#### Java
92108

93109
```java
94-
110+
class Solution {
111+
public int smallestAbsent(int[] nums) {
112+
Set<Integer> s = new HashSet<>();
113+
int sum = 0;
114+
for (int x : nums) {
115+
s.add(x);
116+
sum += x;
117+
}
118+
int ans = Math.max(1, sum / nums.length + 1);
119+
while (s.contains(ans)) {
120+
++ans;
121+
}
122+
return ans;
123+
}
124+
}
95125
```
96126

97127
#### C++
98128

99129
```cpp
100-
130+
class Solution {
131+
public:
132+
int smallestAbsent(vector<int>& nums) {
133+
unordered_set<int> s;
134+
int sum = 0;
135+
for (int x : nums) {
136+
s.insert(x);
137+
sum += x;
138+
}
139+
int ans = max(1, sum / (int) nums.size() + 1);
140+
while (s.contains(ans)) {
141+
++ans;
142+
}
143+
return ans;
144+
}
145+
};
101146
```
102147
103148
#### Go
104149
105150
```go
151+
func smallestAbsent(nums []int) int {
152+
s := map[int]bool{}
153+
sum := 0
154+
for _, x := range nums {
155+
s[x] = true
156+
sum += x
157+
}
158+
ans := max(1, sum/len(nums)+1)
159+
for s[ans] {
160+
ans++
161+
}
162+
return ans
163+
}
164+
```
106165

166+
#### TypeScript
167+
168+
```ts
169+
function smallestAbsent(nums: number[]): number {
170+
const s = new Set<number>(nums);
171+
const sum = nums.reduce((a, b) => a + b, 0);
172+
let ans = Math.max(1, Math.floor(sum / nums.length) + 1);
173+
while (s.has(ans)) {
174+
ans++;
175+
}
176+
return ans;
177+
}
107178
```
108179

109180
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int smallestAbsent(vector<int>& nums) {
4+
unordered_set<int> s;
5+
int sum = 0;
6+
for (int x : nums) {
7+
s.insert(x);
8+
sum += x;
9+
}
10+
int ans = max(1, sum / (int) nums.size() + 1);
11+
while (s.contains(ans)) {
12+
++ans;
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func smallestAbsent(nums []int) int {
2+
s := map[int]bool{}
3+
sum := 0
4+
for _, x := range nums {
5+
s[x] = true
6+
sum += x
7+
}
8+
ans := max(1, sum/len(nums)+1)
9+
for s[ans] {
10+
ans++
11+
}
12+
return ans
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int smallestAbsent(int[] nums) {
3+
Set<Integer> s = new HashSet<>();
4+
int sum = 0;
5+
for (int x : nums) {
6+
s.add(x);
7+
sum += x;
8+
}
9+
int ans = Math.max(1, sum / nums.length + 1);
10+
while (s.contains(ans)) {
11+
++ans;
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def smallestAbsent(self, nums: List[int]) -> int:
3+
s = set(nums)
4+
ans = max(1, sum(nums) // len(nums) + 1)
5+
while ans in s:
6+
ans += 1
7+
return ans
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function smallestAbsent(nums: number[]): number {
2+
const s = new Set<number>(nums);
3+
const sum = nums.reduce((a, b) => a + b, 0);
4+
let ans = Math.max(1, Math.floor(sum / nums.length) + 1);
5+
while (s.has(ans)) {
6+
ans++;
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)