Skip to content

Commit 4b2f4f4

Browse files
committed
test: 3678, 3679, 3681 solution
py, c++, go, java
1 parent 8d90ab5 commit 4b2f4f4

27 files changed

+960
-7
lines changed

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"daily": "966",
3-
"plans": ["3674", "problems", "3675", "problems", "3676", "problems"]
3+
"plans": ["3678", "problems", "3679", "problems", "3681", "problems"]
44
}

golang/problems_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package golang
22

33
import (
4-
problem3674 "leetCode/problems/problems_3674"
5-
problem3675 "leetCode/problems/problems_3675"
6-
problem3676 "leetCode/problems/problems_3676"
4+
problem3678 "leetCode/problems/problems_3678"
5+
problem3679 "leetCode/problems/problems_3679"
6+
problem3681 "leetCode/problems/problems_3681"
77
"testing"
88
)
99

1010
func TestSolutions(t *testing.T) {
11-
TestEach(t, "3674", "problems", problem3674.Solve)
12-
TestEach(t, "3675", "problems", problem3675.Solve)
13-
TestEach(t, "3676", "problems", problem3676.Solve)
11+
TestEach(t, "3678", "problems", problem3678.Solve)
12+
TestEach(t, "3679", "problems", problem3679.Solve)
13+
TestEach(t, "3681", "problems", problem3681.Solve)
1414
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
using namespace std;
5+
using json = nlohmann::json;
6+
7+
class Solution {
8+
public:
9+
int smallestAbsent(const vector<int> &nums) {
10+
unordered_set<int> s;
11+
int sm = 0;
12+
int mx = 0;
13+
int n = nums.size();
14+
for (const auto &num : nums) {
15+
s.insert(num);
16+
sm += num;
17+
mx = max(mx, num);
18+
}
19+
int avg = ceil(static_cast<double>(sm + 1) / n);
20+
for (int i = max(1, avg); i <= mx + 1; ++i) {
21+
if (!s.contains(i)) {
22+
return i;
23+
}
24+
}
25+
return 1;
26+
}
27+
};
28+
29+
json leetcode::qubh::Solve(string input_json_values) {
30+
vector<string> inputArray;
31+
size_t pos = input_json_values.find('\n');
32+
while (pos != string::npos) {
33+
inputArray.push_back(input_json_values.substr(0, pos));
34+
input_json_values = input_json_values.substr(pos + 1);
35+
pos = input_json_values.find('\n');
36+
}
37+
inputArray.push_back(input_json_values);
38+
39+
Solution solution;
40+
vector<int> nums = json::parse(inputArray.at(0));
41+
return solution.smallestAbsent(nums);
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problems.problems_3678;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
6+
7+
8+
public class Solution extends BaseSolution {
9+
public int smallestAbsent(int[] nums) {
10+
Set<Integer> used = new HashSet<>();
11+
int sm = 0, mx = 0;
12+
for (int num: nums) {
13+
used.add(num);
14+
sm += num;
15+
mx = Math.max(num, mx);
16+
}
17+
int avg = (int)Math.ceil((double)(sm + 1) / nums.length);
18+
for (int i = Math.max(1, avg); i <= mx + 1; ++i) {
19+
if (!used.contains(i)) {
20+
return i;
21+
}
22+
}
23+
return 1;
24+
}
25+
26+
@Override
27+
public Object solve(String[] inputJsonValues) {
28+
int[] nums = jsonArrayToIntArray(inputJsonValues[0]);
29+
return JSON.toJSON(smallestAbsent(nums));
30+
}
31+
}

problems/problems_3678/problem.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 3678. Smallest Absent Positive Greater Than Average
2+
3+
<p>You are given an integer array <code>nums</code>.</p>
4+
5+
<p>Return the <strong>smallest absent positive</strong> integer in <code>nums</code> such that it is <strong>strictly greater</strong> than the <strong>average</strong> of all elements in <code>nums</code>.</p>
6+
The <strong>average</strong> of an array is defined as the sum of all its elements divided by the number of elements.
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<div class="example-block">
11+
<p><strong>Input:</strong> <span class="example-io">nums = [3,5]</span></p>
12+
13+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
14+
15+
<p><strong>Explanation:</strong></p>
16+
17+
<ul>
18+
<li>The average of <code>nums</code> is <code>(3 + 5) / 2 = 8 / 2 = 4</code>.</li>
19+
<li>The smallest absent positive integer greater than 4 is 6.</li>
20+
</ul>
21+
</div>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>Input:</strong> <span class="example-io">nums = [-1,1,2]</span></p>
27+
28+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
29+
30+
<p><strong>Explanation:</strong></p>
31+
32+
<ul>
33+
<li>​​​​​​​The average of <code>nums</code> is <code>(-1 + 1 + 2) / 3 = 2 / 3 = 0.667</code>.</li>
34+
<li>The smallest absent positive integer greater than 0.667 is 3.</li>
35+
</ul>
36+
</div>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">nums = [4,-1]</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
44+
45+
<p><strong>Explanation:</strong></p>
46+
47+
<ul>
48+
<li>The average of <code>nums</code> is <code>(4 + (-1)) / 2 = 3 / 2 = 1.50</code>.</li>
49+
<li>The smallest absent positive integer greater than 1.50 is 2.</li>
50+
</ul>
51+
</div>
52+
53+
<p>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
58+
<li><code>-100 &lt;= nums[i] &lt;= 100</code>​​​​​​​</li>
59+
</ul>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 3678. 大于平均值的最小未出现正整数
2+
3+
<p>给你一个整数数组 <code>nums</code>。</p>
4+
5+
<p>返回 <code>nums</code> 中 <strong>严格大于</strong> <code>nums</code> 中所有元素 <strong>平均值</strong> 的 <strong>最小未出现正整数</strong>。</p>
6+
数组的 <strong>平均值</strong> 定义为数组中所有元素的总和除以元素的数量。
7+
8+
<p>&nbsp;</p>
9+
10+
<p><strong class="example">示例 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>输入:</strong> <span class="example-io">nums = [3,5]</span></p>
14+
15+
<p><strong>输出:</strong> <span class="example-io">6</span></p>
16+
17+
<p><strong>解释:</strong></p>
18+
19+
<ul>
20+
<li><code>nums</code> 的平均值是 <code>(3 + 5) / 2 = 8 / 2 = 4</code> 。</li>
21+
<li>大于 4 的最小未出现正整数是 6。</li>
22+
</ul>
23+
</div>
24+
25+
<p><strong class="example">示例 2:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">nums = [-1,1,2]</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<ul>
35+
<li><code>nums</code> 的平均值是 <code>(-1 + 1 + 2) / 3 = 2 / 3 = 0.667</code> 。</li>
36+
<li>大于 0.667 的最小未出现正整数是 3 。</li>
37+
</ul>
38+
</div>
39+
40+
<p><strong class="example">示例 3:</strong></p>
41+
42+
<div class="example-block">
43+
<p><strong>输入:</strong> <span class="example-io">nums = [4,-1]</span></p>
44+
45+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
46+
47+
<p><strong>解释:</strong></p>
48+
49+
<ul>
50+
<li><code>nums</code> 的平均值是 <code>(4 + (-1)) / 2 = 3 / 2 = 1.50</code>。</li>
51+
<li>大于 1.50 的最小未出现正整数是 2。</li>
52+
</ul>
53+
</div>
54+
55+
<p>&nbsp;</p>
56+
57+
<p><strong>提示:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
61+
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
62+
</ul>

problems/problems_3678/solution.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem3678
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"math"
7+
"strings"
8+
)
9+
10+
func smallestAbsent(nums []int) int {
11+
used := make(map[int]bool)
12+
sm := 0
13+
mx := 0
14+
for _, num := range nums {
15+
used[num] = true
16+
sm += num
17+
mx = max(mx, num)
18+
}
19+
avg := int(math.Ceil(float64(sm+1) / float64(len(nums))))
20+
for i := max(avg, 1); i <= mx+1; i++ {
21+
if !used[i] {
22+
return i
23+
}
24+
}
25+
return 1
26+
}
27+
28+
func Solve(inputJsonValues string) any {
29+
inputValues := strings.Split(inputJsonValues, "\n")
30+
var nums []int
31+
32+
if err := json.Unmarshal([]byte(inputValues[0]), &nums); err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
return smallestAbsent(nums)
37+
}

problems/problems_3678/solution.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import math
2+
3+
import solution
4+
from typing import *
5+
6+
7+
class Solution(solution.Solution):
8+
def solve(self, test_input=None):
9+
return self.smallestAbsent(test_input)
10+
11+
def smallestAbsent(self, nums: List[int]) -> int:
12+
s = sum(nums)
13+
st = set(nums)
14+
n = len(nums)
15+
avg = math.ceil((s + 1) / n)
16+
for i in range(max(1, avg), max(nums) + 2):
17+
if i not in st:
18+
return i
19+
return 1

problems/problems_3678/testcase

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
["[3,5]", "[-1,1,2]", "[4,-1]"]
2+
[6, 3, 2]

problems/problems_3678/testcase.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from collections import namedtuple
2+
import testcase
3+
4+
case = namedtuple("Testcase", ["Input", "Output"])
5+
6+
7+
class Testcase(testcase.Testcase):
8+
def __init__(self):
9+
self.testcases = []
10+
self.testcases.append(case(Input=[3, 5], Output=6))
11+
self.testcases.append(case(Input=[-1, 1, 2], Output=3))
12+
self.testcases.append(case(Input=[4, -1], Output=2))
13+
14+
def get_testcases(self):
15+
return self.testcases

0 commit comments

Comments
 (0)