链接:https://www.lintcode.com/zh-cn/problem/count-of-smaller-number/
样例
对于数组 [1,2,7,8,5]
,查询 [1,8,5]
,返回 [0,4,2]
class Solution {
public:
/**
* @param A: An integer array
* @param queries: The query list
* @return: The number of element in the array that are smaller that the given integer
*/
vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
// write your code here
int sizeA=A.size(),sizeQ=queries.size();
if(sizeA<=0 && sizeQ>0) return vector<int>(sizeQ,0);
if(sizeA<=0 && sizeQ<=0) return vector<int>();
vector<int> res;
sort(A.begin(),A.end());
for(auto target:queries)
{
int count=lower_bound(A.begin(),A.end(),target)-A.begin();
res.push_back(count);
}
return res;
}
};