-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
69 lines (66 loc) · 1.65 KB
/
program.cpp
File metadata and controls
69 lines (66 loc) · 1.65 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "../include/pre.h"
#include <map>
class BST
{
public:
int val;
int countOfSmaller; //number of elements in left sub-tree
int count;
BST* left;
BST* right;
public:
BST(int n) : val(n), countOfSmaller(0), count(1), left(nullptr), right(nullptr){}
int insert(int n, int nSmaller = 0)
{
if (n < this->val)
{
this->countOfSmaller++;
if (left == nullptr)
{
left = new BST(n);
return nSmaller;
}
else
{
return left->insert(n, nSmaller);
}
}
else if (n > this->val)
{
nSmaller += (this->countOfSmaller + this->count);
if (right == nullptr)
{
right = new BST(n);
return nSmaller;
}
else
{
return right->insert(n, nSmaller);
}
}
else //(n == this->val)
{
count++;
return nSmaller + this->countOfSmaller;
}
}
~BST(){/*Should have*/}
};
vector<int> countSmaller(vector<int>& nums)
{
if (nums.size() == 0) return {};
vector<int> rst_(1, 0);
auto root = new BST(nums.back());
for (auto it = nums.rbegin() + 1; it != nums.rend(); it++)
{
rst_.push_back(root->insert(*it));
}
std::reverse(rst_.begin(), rst_.end());
return rst_;
}
int main()
{
vector<int> test = {26,78,27,100,33,67,90,23,66,5,38,7,35,23,52,22,83,51,98,69,81,32,78,28,94,13,2,97,3,76,99,51,9,21,84,66,65,36,100,41};
cout << countSmaller(test) << endl;
return 0;
}