[Leetcode] Binary Index Tree

[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

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

Description

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值