-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum Gap.cpp
66 lines (50 loc) · 1.54 KB
/
Maximum Gap.cpp
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
#include <iostream>
#include <vector>
class Solution {
public:
void merge(std::vector<int>& nums, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> leftArray(nums.begin() + left, nums.begin() + left + n1);
vector<int> rightArray(nums.begin() + mid + 1, nums.begin() + mid + 1 + n2);
int i = 0;
int j = 0;
int k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
nums[k++] = leftArray[i++];
} else {
nums[k++] = rightArray[j++];
}
}
while (i < n1) {
nums[k++] = leftArray[i++];
}
while (j < n2) {
nums[k++] = rightArray[j++];
}
}
void mergeSort(std::vector<int>& nums, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
merge(nums, left, mid, right);
}
}
int maximumGap(std::vector<int>& nums) {
int n = nums.size();
if (n < 2) {
return 0;
}
mergeSort(nums, 0, n - 1);
int max_diff = -1;
for (int i = 0; i < n - 1; i++) {
int j = i + 1;
if (nums[j] - nums[i] > max_diff) {
max_diff = nums[j] - nums[i];
}
}
return max_diff;
}
};