-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnearly_sorted.cpp
73 lines (65 loc) · 1.69 KB
/
nearly_sorted.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
// Nearly Sorted
/*
Given an array arr[], where each element is at most k away from its target position,
you need to sort the array optimally.
Example1:
Input: arr[] = [6, 5, 3, 2, 8, 10, 9], k = 3
Output: [2, 3, 5, 6, 8, 9, 10]
Explanation: The sorted array will be 2 3 5 6 8 9 10
Example2:
Input: arr[]= [1, 4, 5, 2, 3, 6, 7, 8, 9, 10], k = 2
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Explanation: The sorted array will be 1 2 3 4 5 6 7 8 9 10
Constraints:
1 ≤ arr.size() ≤ 10^6
1 ≤ k < arr.size()
1 ≤ arr_i ≤ 10^6
Time Complexity: O(n log k)
Auxiliary Space: O(n)
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void nearlySorted(vector<int>& arr, int k) {
int n = arr.size();
priority_queue<int, vector<int>, greater<int>> pq;
for(int i = 0; i < k; ++i)
pq.push(arr[i]);
for(int i = k; i < n; ++i) {
pq.push(arr[i]);
arr[i - k] = pq.top();
pq.pop();
}
for(int i = n - k; i < n; ++i) {
arr[i] = pq.top();
pq.pop();
}
}
};
int main() {
string ts;
getline(cin, ts);
int t = stoi(ts);
while (t--) {
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
string ks;
getline(cin, ks);
int k = stoi(ks);
Solution obj;
obj.nearlySorted(arr, k);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "~" << endl;
}
return 0;
}