Golden interview programmers - 10.10 rank face questions digital stream (map / Fenwick tree)

1. Topic

Suppose you are reading a bunch of integers. Every so often, you want to find the rank number x (less than or equal to the number of values ​​of x).

Implement data structures and algorithms to support these operations, that is to say:

  • Realization track(int x)method, each will read into a digital call the method;

  • Implemented getRankOfNumber(int x)method for returning less than or equal to the number of values of x.

示例:
输入:
["StreamRank", "getRankOfNumber", "track", "getRankOfNumber"]
[[], [1], [0], [0]]
输出:
[null,0,null,1]

提示:
x <= 50000
track 和 getRankOfNumber 方法的调用次数均不超过 2000

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/rank-from-stream-lcci
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

2. Problem Solving

2.1 map

  • map store their number, the writing time complexity THE ( log n ) O(\log n)
  • When the read rank, from front to back traverse together (less than or equal to x) THE ( n ) O (n) time complexity
class StreamRank {
	map<int,int> m;
	int count = 0;
public:
    StreamRank() {}
    
    void track(int x) {
    	m[x]++;
    }
    
    int getRankOfNumber(int x) {
    	count = 0;
    	for(auto& mi : m)
    	{
    		if(x >= mi.first)
    			count += mi.second;
    		else
    			break;
    	}
    	return count;
    }
};

108 ms 13.9 MB

  • previously stored map number less than or equal to its reading time complexity rank THE ( log n ) O(\log n)
  • After insert number, we need to update all of the map's value, time complexity THE ( n ) O (n)
class StreamRank {
	map<int,int> m;
public:
    StreamRank() {}
    
    void track(int x) {
        auto it = m.rbegin();
		for(; it != m.rend(); ++it)
		{
			if(it->first > x)
				it->second++;//有比x大的,他们的value(比它小的个数) +1
			else
				break;
		}
        if(it == m.rend() || (it != m.rend() && it->first == x))
            m[x]++; // map遍历到头了,x不存在,或者x存在
        else
            m[x] = it->second + 1;//遍历没到头,x不存在,x 的 value = 前一个value + 自己
    }
    
    int getRankOfNumber(int x) {
    	if(m.empty() || x < m.begin()->first)
    		return 0;
    	if(m.count(x))
    		return m[x];
    	auto end = m.upper_bound(x);
    	end--;
    	return end->second;
    }
};

120 ms 14 MB


2.2 Fenwick tree

The above solution: at the time complexity of operations is n THE ( n 2 ) O (n ^ 2)

How to optimize: Look at Fenwick tree , a query and modify both time complexity THE ( log n ) O(\log n)

class StreamRank {
    vector<int> v;
    int N = 50002;
public:
    StreamRank() {
        v = vector<int>(N);
    }
    
    void track(int x) {
        update(x+1, 1);
    }
    
    int getRankOfNumber(int x) {
        return query(x+1);
    }
    //-----树状数组-------
    int lowbit(int x)
    {
        return x&(-x);
    }
    void update(int i, int delta)
    {
        for( ; i < N; i += lowbit(i))
            v[i] += delta;
    }
    int query(int i)
    {
        int sum = 0;
        for( ; i > 0; i -= lowbit(i))
            sum += v[i];
        return sum;
    }
};

44 ms 20.6 MB

Published 799 original articles · won praise 1360 · Views 350,000 +

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/105247859