-
Notifications
You must be signed in to change notification settings - Fork 0
/
K-diff Pairs in an Array
171 lines (160 loc) · 5.18 KB
/
K-diff Pairs in an Array
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
LeetCode 532:
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
Example 1:
Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Note:
The pairs (i, j) and (j, i) count as the same pair.
The length of the array won't exceed 10,000.
All the integers in the given input belong to the range: [-1e7, 1e7].
Solution:
Language: C++
Tag: sort binary search hashmap
*/
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
if (k < 0 || nums.size() <= 1) {
return 0;
}
// 1. sort + binary search-- O(nlogn) time + O(1) space
sort(nums.begin(), nums.end(), less<int>());
//quicksort(nums, 0, nums.size() - 1);
//heapsort(nums);
//int *temp = new int[nums.size()];
//mergesort(nums, 0, nums.size() - 1, temp);
//free(temp);
int count = 0;
for (int i = 0; i < nums.size() - 1; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int start = i + 1, end = nums.size() - 1;
int target = nums[i] + k;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (nums[start] == target || nums[end] == target) {
++count;
}
}
return count;
/*
// 2. hashmap -- O(n) time + O(n) space
unordered_map<int, int> hash;
for (int num : nums) {
if (hash.find(num) == hash.end()) {
hash[num] = 1;
} else {
++hash[num];
}
}
int count = 0;
for (pair<int, int> item : hash) {
if (k == 0 && item.second >= 2) {
++count;
} else if (k > 0) {
if (hash.find(item.first + k) != hash.end()) {
++count;
}
}
}
return count;
*/
}
void quicksort(vector<int> &nums, int start, int end) {
if (start >= end) {
return;
}
int left = start, right = end, pivot = nums[(start + end) / 2];
while (left <= right) {
while (left <= right && nums[left] < pivot) {
++left;
}
while (left <= right && nums[right] > pivot) {
--right;
}
if (left <= right) {
swap(nums[left++], nums[right--]);
}
}
quicksort(nums, start, right);
quicksort(nums, left, end);
}
void mergesort(vector<int> &nums, int start, int end, int *temp) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
mergesort(nums, start, mid, temp);
mergesort(nums, mid + 1, end, temp);
merge(nums, start, end, temp);
}
void merge(vector<int> &nums, int start, int end, int *temp) {
int mid = start + (end - start) / 2;
int left = start, right = mid + 1, idx = start;
while (left <= mid && right <= end) {
if (nums[left] <= nums[right]) {
temp[idx++] = nums[left++];
} else {
temp[idx++] = nums[right++];
}
}
while (left <= mid) {
temp[idx++] = nums[left++];
}
while (right <= end) {
temp[idx++] = nums[right++];
}
for (int i = start; i <= end; ++i) {
nums[i] = temp[i];
}
}
void heapsort(vector<int> &nums) {
int size = nums.size();
for (int i = (size - 1) / 2; i >= 0; --i) {
siftdown(nums, i, size);
}
while (size > 0) {
swap(nums[0], nums[--size]);
siftdown(nums, 0, size);
}
int left = 0, right = nums.size() - 1;
while (left < right) {
swap(nums[left++], nums[right--]);
}
}
void siftdown(vector<int> &nums, int father, int size) {
while (father < size) {
int min_idx = father;
int left = father * 2 + 1, right = father * 2 + 2;
if (left < size && nums[left] < nums[min_idx]) {
min_idx = left;
}
if (right < size && nums[right] < nums[min_idx]) {
min_idx = right;
}
if (father == min_idx) {
break;
}
swap(nums[father], nums[min_idx]);
father = min_idx;
}
}
};