Skip to content

Commit

Permalink
Time: 128 ms (96.40%), Space: 76 MB (88.94%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitkekri committed Nov 12, 2021
1 parent ece3976 commit 8342b55
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:

void update(int idx, vector<int> &BIT) {
while(idx < BIT.size()) {
BIT[idx]++;
idx += idx & -idx;
}
}

int answer(int idx, vector<int> &BIT) {
int small = 0;
while(idx > 0) {
small += BIT[idx];
idx -= idx & -idx;
}
return small;
}

vector<int> countSmaller(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, 0);
vector<int> BIT(20005, 0);

for(int i = n-1; i >= 0; i--) {
res[i] = answer(nums[i] + 10001 - 1, BIT);
update(nums[i] + 10001, BIT);
}
return res;
}
};

0 comments on commit 8342b55

Please sign in to comment.