From 360661224b007d3fa427119e5581db47baecbb38 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 15 Sep 2025 22:11:26 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3678 No.3678.Smallest Absent Positive Greater Than Average --- .../README.md | 79 ++++++++++++++++++- .../README_EN.md | 79 ++++++++++++++++++- .../Solution.cpp | 16 ++++ .../Solution.go | 13 +++ .../Solution.java | 15 ++++ .../Solution.py | 7 ++ .../Solution.ts | 9 +++ 7 files changed, 210 insertions(+), 8 deletions(-) create mode 100644 solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.cpp create mode 100644 solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.go create mode 100644 solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.java create mode 100644 solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.py create mode 100644 solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.ts diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README.md b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README.md index 10644d45b8b76..654127da6ca3c 100644 --- a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README.md +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README.md @@ -81,32 +81,103 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3678.Sm -### 方法一 +### 方法一:哈希表 + +我们用一个哈希表 $\textit{s}$ 来记录数组 $\textit{nums}$ 中出现过的元素。 + +然后,我们计算数组 $\textit{nums}$ 的平均值 $\textit{avg}$,并将答案 $\textit{ans}$ 初始化为 $\max(1, \lfloor \textit{avg} \rfloor + 1)$。 + +如果 $\textit{ans}$ 在 $\textit{s}$ 中出现过,那么我们将 $\textit{ans}$ 加一,直到 $\textit{ans}$ 不在 $\textit{s}$ 中出现过为止。 + +最后,返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def smallestAbsent(self, nums: List[int]) -> int: + s = set(nums) + ans = max(1, sum(nums) // len(nums) + 1) + while ans in s: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int smallestAbsent(int[] nums) { + Set s = new HashSet<>(); + int sum = 0; + for (int x : nums) { + s.add(x); + sum += x; + } + int ans = Math.max(1, sum / nums.length + 1); + while (s.contains(ans)) { + ++ans; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int smallestAbsent(vector& nums) { + unordered_set s; + int sum = 0; + for (int x : nums) { + s.insert(x); + sum += x; + } + int ans = max(1, sum / (int) nums.size() + 1); + while (s.contains(ans)) { + ++ans; + } + return ans; + } +}; ``` #### Go ```go +func smallestAbsent(nums []int) int { + s := map[int]bool{} + sum := 0 + for _, x := range nums { + s[x] = true + sum += x + } + ans := max(1, sum/len(nums)+1) + for s[ans] { + ans++ + } + return ans +} +``` +#### TypeScript + +```ts +function smallestAbsent(nums: number[]): number { + const s = new Set(nums); + const sum = nums.reduce((a, b) => a + b, 0); + let ans = Math.max(1, Math.floor(sum / nums.length) + 1); + while (s.has(ans)) { + ans++; + } + return ans; +} ``` diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README_EN.md b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README_EN.md index 7eaec2819603e..129df4fdf2bdf 100644 --- a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README_EN.md +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/README_EN.md @@ -78,32 +78,103 @@ The average of an array is defined as the sum of all its elemen -### Solution 1 +### Solution 1: Hash Map + +We use a hash map $\textit{s}$ to record the elements that appear in the array $\textit{nums}$. + +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)$. + +If $\textit{ans}$ appears in $\textit{s}$, we increment $\textit{ans}$ until it no longer appears in $\textit{s}$. + +Finally, we return $\textit{ans}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. #### Python3 ```python - +class Solution: + def smallestAbsent(self, nums: List[int]) -> int: + s = set(nums) + ans = max(1, sum(nums) // len(nums) + 1) + while ans in s: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int smallestAbsent(int[] nums) { + Set s = new HashSet<>(); + int sum = 0; + for (int x : nums) { + s.add(x); + sum += x; + } + int ans = Math.max(1, sum / nums.length + 1); + while (s.contains(ans)) { + ++ans; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int smallestAbsent(vector& nums) { + unordered_set s; + int sum = 0; + for (int x : nums) { + s.insert(x); + sum += x; + } + int ans = max(1, sum / (int) nums.size() + 1); + while (s.contains(ans)) { + ++ans; + } + return ans; + } +}; ``` #### Go ```go +func smallestAbsent(nums []int) int { + s := map[int]bool{} + sum := 0 + for _, x := range nums { + s[x] = true + sum += x + } + ans := max(1, sum/len(nums)+1) + for s[ans] { + ans++ + } + return ans +} +``` +#### TypeScript + +```ts +function smallestAbsent(nums: number[]): number { + const s = new Set(nums); + const sum = nums.reduce((a, b) => a + b, 0); + let ans = Math.max(1, Math.floor(sum / nums.length) + 1); + while (s.has(ans)) { + ans++; + } + return ans; +} ``` diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.cpp b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.cpp new file mode 100644 index 0000000000000..318a27c79966f --- /dev/null +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int smallestAbsent(vector& nums) { + unordered_set s; + int sum = 0; + for (int x : nums) { + s.insert(x); + sum += x; + } + int ans = max(1, sum / (int) nums.size() + 1); + while (s.contains(ans)) { + ++ans; + } + return ans; + } +}; diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.go b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.go new file mode 100644 index 0000000000000..0c3073fc142ad --- /dev/null +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.go @@ -0,0 +1,13 @@ +func smallestAbsent(nums []int) int { + s := map[int]bool{} + sum := 0 + for _, x := range nums { + s[x] = true + sum += x + } + ans := max(1, sum/len(nums)+1) + for s[ans] { + ans++ + } + return ans +} diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.java b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.java new file mode 100644 index 0000000000000..4ba579a68c09f --- /dev/null +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int smallestAbsent(int[] nums) { + Set s = new HashSet<>(); + int sum = 0; + for (int x : nums) { + s.add(x); + sum += x; + } + int ans = Math.max(1, sum / nums.length + 1); + while (s.contains(ans)) { + ++ans; + } + return ans; + } +} diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.py b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.py new file mode 100644 index 0000000000000..d1d77666549b9 --- /dev/null +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def smallestAbsent(self, nums: List[int]) -> int: + s = set(nums) + ans = max(1, sum(nums) // len(nums) + 1) + while ans in s: + ans += 1 + return ans diff --git a/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.ts b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.ts new file mode 100644 index 0000000000000..216ceea78a62b --- /dev/null +++ b/solution/3600-3699/3678.Smallest Absent Positive Greater Than Average/Solution.ts @@ -0,0 +1,9 @@ +function smallestAbsent(nums: number[]): number { + const s = new Set(nums); + const sum = nums.reduce((a, b) => a + b, 0); + let ans = Math.max(1, Math.floor(sum / nums.length) + 1); + while (s.has(ans)) { + ans++; + } + return ans; +}