Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai
in the array, count the number of element before this element Ai
is smaller than it and return count number array.
Notice
We suggest you finish problem Segment Tree Build, Segment Tree Query IIand Count of Smaller Number before itself I first.
Example
不用线段树, 竟然也过了~~~
For array [1,2,7,8,5]
, return [0,1,2,3,2]
class Solution {
public:
/**
* @param A: An integer array
* @return: Count the number of element before this element 'ai' is
* smaller than it and return count number array
*/
vector<int> countOfSmallerNumberII(vector<int> &A) {
// write your code here
vector <int> temp;
vector<int> res;
for (int i = 0; i < A.size(); i++) {
auto itr = lower_bound(temp.begin(), temp.end(), A[i]);
int count = itr - temp.begin();
res.push_back(count);
temp.insert(itr, A[i]);
}
return res;
}
};