-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path719. Find K-th Smallest Pair Distance.cpp
193 lines (163 loc) · 6.3 KB
/
719. Find K-th Smallest Pair Distance.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//TLE
//16 / 19 test cases passed.
class Solution {
public:
priority_queue<int, vector<int>> pq;
int findSmallerDistances(vector<int>& nums, int& target){
pq = priority_queue<int, vector<int>>(); //clear
int n = nums.size();
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
if(nums[j] - nums[i] <= target){
pq.push(nums[j] - nums[i]);
}
}
}
return pq.size();
};
int smallestDistancePair(vector<int>& nums, int k) {
int left = 0;
int right = *max_element(nums.begin(), nums.end()) - *min_element(nums.begin(), nums.end());
sort(nums.begin(), nums.end());
int mid, val;
while(left <= right){
mid = left + (right - left)/2;
// cout << left << ", " << mid << ", " << right << ": ";
int smallerCount = findSmallerDistances(nums, mid);
// cout << val << endl;
if(smallerCount == k){
return pq.top();
}else if(smallerCount < k){
left = mid+1;
}else if(smallerCount > k){
right = mid-1;
while(pq.size() > k){
pq.pop();
}
return pq.top();
}
}
while(pq.size() > k){
pq.pop();
}
return pq.top();
}
};
//Approach #1: Heap [Time Limit Exceeded]
//TLE
//16 / 19 test cases passed.
//time: push: O(NlogN) + pop: O(klogN) -> O((N+k)logN)
//space: O(N)
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
auto comp = [&nums](const vector<int>& a, const vector<int>& b){
//use > because we want the smaller to be popped earlier
return nums[a[1]] - nums[a[0]] > nums[b[1]] - nums[b[0]];
};
priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> pq(comp);
int n = nums.size();
for(int i = 0; i+1 < n; i++){
pq.push(vector<int>{i, i+1});
// cout << i << ", " << i+1 << endl;
}
vector<int> cur;
k--;
//pop first k-1 pairs
while(k-- > 0){
cur = pq.top(); pq.pop();
// cout << cur[0] << ", " << cur[1] << endl;
if(cur[1]+1 < n){
pq.push({cur[0], cur[1]+1});
}
}
//the kth pair
cur = pq.top();
// cout << "final: " << cur[0] << ", " << cur[1] << endl;
return nums[cur[1]] - nums[cur[0]];
}
};
//not understand
//Approach #2: Binary Search + Prefix Sum [Accepted]
//Runtime: 56 ms, faster than 22.59% of C++ online submissions for Find K-th Smallest Pair Distance.
//Memory Usage: 36.9 MB, less than 8.33% of C++ online submissions for Find K-th Smallest Pair Distance.
//time: sort: O(NlogN) + set "prefix": O(W) + binary search: O(logW * N), W = nums[n-1] - nums[0]
//space: O(W+N)
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int n = nums.size();
int width = 2 * nums[n-1];
vector<int> multiplicity(n);
//multiplicity[i]: before nums[i], there are how many element equal to nums[i]
// cout << "multiplicity: " << endl;
for(int i = 1; i < n; i++){
if(nums[i] == nums[i-1]){
multiplicity[i] = 1 + multiplicity[i-1];
}
// cout << multiplicity[i] << " ";
}
// cout << endl;
//prefix[i]: how many numbers in nums <= i
vector<int> prefix(width);
// cout << "prefix: " << endl;
int l = 0;
for(int i = 0; i < width; i++){
while(l < n && nums[l] == i) l++;
prefix[i] = l;
// cout << prefix[i] << " ";
}
// cout << endl;
int left = 0;
int right = nums[n-1] - nums[0];
int mid;
int count;
while(left <= right){
mid = left + (right-left)/2;
// cout << "left: " << left << ", mid: " << mid << ", right: " << right << endl;
//count[i]: the count of j (j>i) s.t. nums[j] - nums[i] <= mid
count = 0;
for(int i = 0; i < n; i++){
//multiplicity[i] = count[i] - multiplicity[i]?
count += prefix[nums[i]+mid] - prefix[nums[i]] + multiplicity[i];
// cout << "prefix[" << nums[i]+mid << "]: " << prefix[nums[i]+mid] << ", prefix[" << nums[i] << "]: " << prefix[nums[i]] << ", multiplicity[" << i << "]: " << multiplicity[i] << ", add: " << prefix[nums[i]+mid] - prefix[nums[i]] + multiplicity[i] << ", count: " << count << endl;
}
if(count >= k) right = mid-1;
else left = mid+1;
}
return left;
}
};
//Approach #3: Binary Search + Sliding Window [Accepted]
//Runtime: 20 ms, faster than 77.27% of C++ online submissions for Find K-th Smallest Pair Distance.
//Memory Usage: 10 MB, less than 8.33% of C++ online submissions for Find K-th Smallest Pair Distance.
//time: sort: O(NlogN) + binary search: O(logW * N), W = nums[n-1] - nums[0]
//space: O(1)
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int n = nums.size();
int left = 0;
int right = nums[n-1] - nums[0];
int mid;
int count;
while(left <= right){
mid = left + (right-left)/2;
// cout << "left: " << left << ", mid: " << mid << ", right: " << right << endl;
count = 0;
int slow = 0;
for(int fast = 0; fast < n; fast++){
//notice that "slow" pointer increases monotonically for increasing "fast" pointer!
while(nums[fast] - nums[slow] > mid) slow++;
count += (fast-slow);
// cout << "[" << slow << ", " << fast << "], count: " << count << endl;
}
if(count >= k) right = mid-1;
else left = mid+1;
}
return left;
}
};