forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
102 lines (79 loc) · 1.98 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/// Source : https://leetcode.com/problems/count-of-smaller-numbers-after-self/
/// Author : liuyubobobo
/// Time : 2018-12-03
#include <iostream>
#include <vector>
#include <set>
using namespace std;
/// Using BST
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class BST{
private:
class TreeNode{
public:
int val, sz;
TreeNode *left, *right;
TreeNode(int val): val(val), sz(1), left(NULL), right(NULL){}
};
TreeNode* root;
public:
BST(): root(NULL){}
void add(int x){
root = add(root, x);
}
int smaller_than(int x){
return smaller_than(root, x);
}
private:
TreeNode* add(TreeNode* node, int x){
if(!node)
return new TreeNode(x);
if(x <= node->val)
node->left = add(node->left, x);
else
node->right = add(node->right, x);
node->sz ++;
return node;
}
int smaller_than(TreeNode* node, int x){
if(!node)
return 0;
if(x <= node->val)
return smaller_than(node->left, x);
return (node->left ? node->left->sz : 0) + 1 + smaller_than(node->right, x);
}
};
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> res;
if(nums.size() == 0)
return res;
res.push_back(0);
if(nums.size() == 1)
return res;
BST bst;
bst.add(nums.back());
for(int i = nums.size() - 2; i >= 0; i --){
bst.add(nums[i]);
res.push_back(bst.smaller_than(nums[i]));
}
reverse(res.begin(), res.end());
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> nums1 = {5, 2, 6, 1};
print_vec(Solution().countSmaller(nums1));
// 2 1 1 0
vector<int> nums2 = {2, 0, 1};
print_vec(Solution().countSmaller(nums2));
// 2 0 0
return 0;
}