6-line C++ binary-search-based solution


  • 1
    D

    This is a common solution with a vector storing the sorted elements on the right of the target. It has O(n^2) time in the worst case because the insertion takes O(n) time in the worst case. I just leverage some C++ APIs to make the solution shorter.

        vector<int> countSmaller(vector<int>& nums) {
            vector<int> result(nums.size()), sorted;
            for (int i = nums.size() - 1; i  >= 0; --i) {
                auto it = sorted.insert(lower_bound(sorted.begin(), sorted.end(), nums[i]), nums[i]);
                result[i] = it - sorted.begin();
            }
            return result;
        }
    

  • 0
    S

    @ducalpha
    Though it is a O(N^2) solution, it is simple and concise.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.