-
Notifications
You must be signed in to change notification settings - Fork 3
/
countOfSmallerNumber.cpp
127 lines (123 loc) · 3.04 KB
/
countOfSmallerNumber.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
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
class STNode {
public:
int start;
int end;
int count;
STNode * left;
STNode * right;
STNode(int _start, int _end, int _count){
this->start = _start;
this->end = _end;
this->count = _count;
this->left = NULL;
this->right = NULL;
}
};
class SegmentTree{
public:
STNode * build(vector<int> &A, int start, int end){
if(start > end){
return NULL;
}
STNode* root = new STNode(start, end, 0);
if(start != end){
int middle = start + (end - start) / 2;
root->left = build(A, start, middle);
root->right = build(A, middle+1, end);
root->count = root->left->count + root->right->count;
}
else{
root->count = 0; // initially empty
}
return root;
}
//query the count of value range [start ~ end]
int queryCount(STNode * root, int start, int end){
if(root == NULL){
return 0;
}
if(start == root->start && root->end == end){
return root->count;
}
int middle = root->start + (root->end - root->start) / 2;
int leftCount = 0;
if(start <= middle){
if(middle < end){
leftCount = queryCount(root->left, start, middle);
}
else{
leftCount = queryCount(root->left, start, end);
}
}
int rightCount = 0;
if(middle < end){
if(start <= middle){
rightCount = queryCount(root->right, middle+1, end);
}
else{
rightCount = queryCount(root->right, start, end);
}
}
return leftCount + rightCount;
}
void modify(STNode * root, int index, int value){
if(root == NULL){
return;
}
if(root->start == index && index == root->end){
root->count += value;
return;
}
int middle = root->start + (root->end - root->start) / 2;
if(index <= middle){
modify(root->left, index, value);
}
else {
modify(root->right, index, value);
}
root->count = root->left->count + root->right->count;
}
};
class Solution {
public:
/**
* @param A: An integer array
* @return: The number of element in the array that
* are smaller that the given integer
*/
// V1 循环方法 O(N * K)
vector<int> countOfSmallerNumber1(vector<int> &A, vector<int> &queries) {
// write your code here
vector<int> result;
int n = A.size();
int k = queries.size();
for(int i = 0; i < k; i++){
int target = queries[i];
int count = 0;
for(int j = 0; j < n; j++){
if(A[j] < target){
count++;
}
}
result.push_back(count);
}
return result;
}
// V2
vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
vector<int> ans;
SegmentTree tree;
STNode * root = tree.build(A, 0, 10000);
for(int i = 0; i < A.size(); i++){
tree.modify(root, A[i], 1);
}
for(int k = 0; k < queries.size(); k++){
int count = 0;
if(queries[k] > 0){
count = tree.queryCount(root, 0, queries[k]-1);
}
ans.push_back(count);
}
return ans;
}
};