LintCode_249 Count of Smaller Number before itself

来源:互联网 发布:mac如何切换搜狗输入法 编辑:程序博客网 时间:2024/06/10 14:16

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;    }};




0 0
原创粉丝点击