-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (46 loc) · 1.15 KB
/
main.cpp
File metadata and controls
49 lines (46 loc) · 1.15 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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> pivotArray(vector<int>& nums, int pivot)
{
queue<int> left;
queue<int> right;
int pivot_count = 0;
for (int i = 0; i < (int)nums.size(); ++i)
{
if (nums[i] < pivot)
left.push(nums[i]);
else if (nums[i] > pivot)
right.push(nums[i]);
else
pivot_count++;
}
vector<int> answer;
answer.reserve(nums.size());
while (!left.empty())
{
answer.push_back(left.front());
left.pop();
}
for (int i = 0; i < pivot_count; ++i)
answer.push_back(pivot);
while (!right.empty())
{
answer.push_back(right.front());
right.pop();
}
return answer;
}
};
int main()
{
vector<int> nums = {9,12,5,10,14,3,10};
// vector<int> nums = {-8,0,7,-7,19,15,6,-5,-10,11,-6,-5,20,3,-6,10,-2};
vector<int> answer = Solution().pivotArray(nums, 10);
for (int el: answer)
cout << el << ' ';
cout << '\n';
return 0;
}