[Leetcode] Binary Index Tree
In this article we will discuss the Binary Indexed Trees structure. According to Peter M. Fenwick, this structure was first used for data compression. Now it is often used for storing frequencies and manipulating cumulative frequency tables.
Let’s define the following problem: We have n boxes. Possible queries are
1. add marble to box i
2. sum marbles from box k to box l
The naive solution has time complexity of O(1) for query 1 and O(n) for query 2. Suppose we make m queries. The worst case (when all queries are 2) has time complexity O(n * m). Using some data structure (i.e. RMQ) we can solve this problem with the worst case time complexity of O(m log n). Another approach is to use Binary Indexed Tree data structure, also with the worst time complexity O(m log n) — but Binary Indexed Trees are much easier to code, and require less memory space, than RMQ.
The detail about the algorithm and data structure can be found in Binary Index Tree .
307. Range Sum Query - Mutable
After understanding BIT, this question can be easily solved.
#include <vector>
#include <iostream>
using namespace std;
class NumArray {
private:
void _update(vector<int>& BIT, int index, int val) {
while (index < BIT.size()) {
BIT[index] += val;
index += (index & -index);
}
}
int _sumRange(vector<int>& BIT, int index) {
int count = 0;
while (index > 0) {
count += BIT[index];
index -= (index & -index);
}
return count;
}
vector<int> BIT;
vector<int> nums;
public:
NumArray(vector<int> nums) {
BIT = vector<int>(nums.size() + 1, 0);
this->nums = nums;
for (int i = 0; i < nums.size(); i++) {
_update(BIT, i + 1, nums[i]);
}
}
void update(int i, int val) {
_update(BIT, i + 1, val - nums[i]);
nums[i] = val;
}
int sumRange(int i, int j) {
return _sumRange(BIT, j + 1) - _sumRange(BIT, i);
}
};
int main() {
vector<int> vec{1, 3, 5};
NumArray obj = NumArray(vec);
cout << obj.sumRange(0, 2) << endl;
obj.update(1, 2);
cout << obj.sumRange(0, 2) << endl;
return 0;
}
Time Complexity: O(nlogn)
493. Reverse Pairs
It is obviously that we can solve the problem by brutal force, but it will be TLE, undoubtedly. So we need to find another good method to solve that. Actually, we can use BIT to do so. Even if the question isn’t similar to the description of the question which BIT can solved, but we can adjust the algorithm to solve this question. The detail can be found Solution.
class Solution {
private:
int read(vector<int>& BIT, int index) {
int count = 0;
while (index < BIT.size()) {
count += BIT[index];
index += (index & -index);
}
return count;
}
void update(vector<int>& BIT, int index, int val) {
while (index > 0) {
BIT[index] += val;
index -= (index & -index);
}
}
public:
int reversePairs(vector<int>& nums) {
vector<int> index_nums = nums;
sort(index_nums.begin(), index_nums.end());
vector<int> BITS(nums.size() + 1, 0);
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += read(BITS, lower_bound(index_nums.begin(), index_nums.end(), nums[i] * 2LL + 1) - index_nums.begin() + 1);
update(BITS, lower_bound(index_nums.begin(), index_nums.end(), nums[i]) - index_nums.begin() + 1, 1);
}
return count;
}
};
Time complexity: O(nlogn)