-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcount_of_smaller_numbers_after_itself.cpp
More file actions
81 lines (77 loc) · 1.89 KB
/
Copy pathcount_of_smaller_numbers_after_itself.cpp
File metadata and controls
81 lines (77 loc) · 1.89 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
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Node{
public:
Node * left;
Node * right;
int val;
int count;// count of smaller
int dup;
Node(int v){
this->val = v;
this->count = 0;
this->dup = 1;
this->left = NULL;
this->right = NULL;
}
};
class Solution {
private:
int insert(Node * root, int val){
int smallCount = 0;
while(true) {
if(val == root->val){
smallCount += root->count;
root->dup++;
return smallCount;
}
else if(val < root->val) {
root->count++;
if(root->left == NULL) {
root->left = new Node(val);
return smallCount;
}
root = root->left;
}
else {
smallCount += root->dup + root->count;
if(root->right == NULL) {
root->right = new Node(val);
return smallCount;
}
root = root->right;
}
}
cout<<"insert "<<root->val<<" ["<<root->count<<","<<root->dup<<"]"<<endl;
return smallCount;
}
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> result;
int n = nums.size();
if(n == 0){
return result;
}
Node * root = new Node(nums[n-1]);
result.push_back(0);
for(int i = nums.size()-2; i >= 0; i--){
int count = insert(root, nums[i]);
result.push_back(count);
}
// reverse result
reverse(result.begin(), result.end());
return result;
}
};
int main(void){
Solution sol;
vector<int> nums = {5, 2, 6, 1};
vector<int> result = sol.countSmaller(nums);
for(int x : result){
cout<<x<<" ";
}
cout<<endl;
return 0;
}