-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSmallerNumbersAfterSelf.cpp
More file actions
72 lines (64 loc) · 1.4 KB
/
CountSmallerNumbersAfterSelf.cpp
File metadata and controls
72 lines (64 loc) · 1.4 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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
struct TreeNode{
TreeNode* left;
TreeNode* right;
int value;
int count;
TreeNode(int val):count(0), value(val), left(NULL), right(NULL){}
};
public:
TreeNode* insert(TreeNode* root, TreeNode* node){
if(root == NULL)
return node;
if(root->value > node->value){
root->count++;
root->left = insert(root->left, node);
} else {
root->right = insert(root->right, node);
}
return root;
}
int search(TreeNode* root, int value){
TreeNode* node = root;
int count = 0;
while(node != NULL){
if(node->value == value){
count += node->count;
return count;
}
if(node->value > value)
node = node->left;
else{
count += node->count + 1;
node = node->right;
}
}
cout << "no such value" << endl;
return 0;
}
vector<int> countSmaller(vector<int>& nums) {
vector<int> res;
TreeNode* root = NULL;
for(auto iter=nums.rbegin(); iter!=nums.rend(); iter++){
int n = *iter;
TreeNode* node = new TreeNode(n);
root = insert(root, node);
res.push_back(search(root, n));
}
reverse(res.begin(), res.end());
return res;
}
};
int main(int argc, char *argv[]) {
vector<int> v{5,2,6,1};
Solution sol;
vector<int> res = sol.countSmaller(v);
for(auto &i : res)
cout << i << " ";
return 0;
}