-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily16.cpp
More file actions
69 lines (52 loc) · 1.75 KB
/
Copy pathdaily16.cpp
File metadata and controls
69 lines (52 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Solution 1
class Solution {
public:
int minDifference(vector<int>& nums) {
if (nums.size() <= 4) return 0; // If there are 4 or fewer elements, we can make all of them the same
std::sort(nums.begin(), nums.end());
// We need to consider changing up to three largest or smallest numbers
int n = nums.size();
int result = INT_MAX;
// We have four possibilities:
// 1. Change three largest numbers
// 2. Change two largest and one smallest
// 3. Change one largest and two smallest
// 4. Change three smallest numbers
result = std::min(result, nums[n-4] - nums[0]);
result = std::min(result, nums[n-3] - nums[1]);
result = std::min(result, nums[n-2] - nums[2]);
result = std::min(result, nums[n-1] - nums[3]);
return result;
}
};
// Solution 2
static int _ = [](){ std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr); return 0;}();
class Solution {
public:
int minDifference(vector<int>& nums) {
if (nums.size() <= 4) {
return 0;
}
std::nth_element(nums.begin(), nums.begin() + 4, nums.end());
std::nth_element(nums.begin() + 4, nums.end() - 4, nums.end());
std::sort(nums.begin(), nums.begin() + 4);
std::sort(nums.end() - 4, nums.end());
for (int i = 0; i < nums.size(); ++i) {
//std::cout << nums[i] << " ";
}
int min = INT_MAX;
for (int left = 0; left <= 3; ++left) {
int right = 3 - left;
min = std::min(min, std::abs(nums[nums.size() - 1 - right] - nums[left]));
}
return min;
}
};
/*
0,1,5,10,14
10,14 4
5,10 5
1,5 4
0,1 1
*/