-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountofSmallerNumbersAfterSelf.cpp
More file actions
117 lines (104 loc) · 3.01 KB
/
countofSmallerNumbersAfterSelf.cpp
File metadata and controls
117 lines (104 loc) · 3.01 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
// Source: https://leetcode.com/problems/count-of-smaller-numbers-after-self/
// Author: Miao Zhang
// Date: 2021-02-01
/********************************************************
* original:[5, 2, 6, 1]
* reverse: [1, 6, 2, 5]
* sorted: [1, 2, 5, 6]
* ranked: [1, 4, 2, 3]
* res: [0, 1, 1, 2]
********************************************************/
class BinaryIndexTree {
public:
BinaryIndexTree(int n): sums_(n + 1, 0) {
}
void update(int i, int delta) {
while (i < sums_.size()) {
sums_[i] += delta;
i += lowbit(i);
}
}
int query(int i) const {
int sum = 0;
while (i > 0) {
sum += sums_[i];
i -= lowbit(i);
}
return sum;
}
private:
static inline int lowbit(int x) { return x & (-x); }
vector<int> sums_;
};
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
set<int> sorted(nums.begin(), nums.end());
unordered_map<int, int> ranks;
int rank = 0;
for (const int num: sorted) {
ranks[num] = ++rank;
}
vector<int> res;
BinaryIndexTree tree(ranks.size());
for (int i = nums.size() - 1; i >= 0; i--) {
res.push_back(tree.query(ranks[nums[i]] - 1));
tree.update(ranks[nums[i]], 1);
}
reverse(res.begin(), res.end());
return res;
}
};
/********************************************************
* BST: Binary Search Tree
* original:[5, 2, 6, 1]
* reverse: [1, 6, 2, 5]
********************************************************/
struct BSTNode {
int val;
int count;
int left_count;
BSTNode* left;
BSTNode* right;
BSTNode(int val): val(val), count(1), left_count(0), left{nullptr}, right{nullptr} {
}
~BSTNode() {
delete left;
delete right;
}
int less_or_equal() const { return count + left_count; }
};
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
if (nums.empty()) return {};
std::reverse(nums.begin(), nums.end());
std::unique_ptr<BSTNode> root(new BSTNode(nums[0]));
vector<int> res{0};
for (int i = 1; i < nums.size(); i++) {
res.push_back(insert(root.get(), nums[i]));
}
std::reverse(res.begin(), res.end());
return res;
}
private:
int insert(BSTNode* root, int val) {
if (root->val == val) {
++root->count;
return root->left_count;
} else if (val < root->val) {
++root->left_count;
if (root->left == nullptr) {
root->left = new BSTNode(val);
return 0;
}
return insert(root->left, val);
} else {
if (root->right == nullptr) {
root->right = new BSTNode(val);
return root->less_or_equal();
}
return root->less_or_equal() + insert(root->right, val);
}
}
};