-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcountSmaller.cpp
More file actions
136 lines (129 loc) · 3.12 KB
/
countSmaller.cpp
File metadata and controls
136 lines (129 loc) · 3.12 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//http://traceformula.blogspot.com/2015/12/count-of-smaller-numbers-after-self.html
//不明白
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 Solution1 {
private:
int insert(Node* root, int val) {
int smallCount = 0;
Node* cur = root;
//不明白
while (true) {
if (cur->val < val) {
smallCount += cur->count + cur->dup;
if (cur->right == NULL) {
cur->right = new Node(val);
return smallCount;
}
else {
cur = cur->right;
}
}
else if (cur->val > val) {
cur->count++;
if (cur->left == NULL) {
cur->left = new Node(val);
return smallCount;
}
else {
cur = cur->left;
}
}
// equal
else {
smallCount += cur->count;
cur->dup++;
return smallCount;
}
}
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;
}
};
////////////////////////////
// V2 : BIT(Binary Index Tree)
class Solution {
private:
int makePositive(vector<int>& nums){
int minValue = nums[0];
int maxValue = nums[0];
for(int x : nums){
minValue = min(minValue, x);
maxValue = max(maxValue, x);
}
if(minValue <= 0){
minValue = -minValue + 1;
maxValue += minValue;
for(int i = 0; i < nums.size(); i++){
nums[i] += minValue;
}
}
return maxValue;
}
vector<int> bit;
void build(int n){
bit.assign(n+1, 0);
}
int get(int index){
index = index+1;
int sum = 0;
while(index > 0){
sum += bit[index];
index -= index & -index;
}
return sum;
}
void set(int index, int value){
index = index+1;
while(index < bit.size()){
bit[index] += value;
index += (index & -index);
}
}
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> result;
if(nums.size() == 0){
return result;
}
// use nums[i] as index!!!
int maxn = makePositive(nums);
build(maxn+1);
for(int i = nums.size()-1; i >= 0; i--){
int count = get(nums[i]-1);
result.push_back(count);
set(nums[i], 1);
}
reverse(result.begin(), result.end());
return result;
}
};