Count Of Smaller Numbers After Self Problem


Description

LeetCode Problem 315.

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example 1:

1
2
3
4
5
6
7
Input: nums = [5,2,6,1]
Output: [2,1,1,0]
Explanation:
To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.

Example 2:

1
2
Input: nums = [-1]
Output: [0]

Example 3:

1
2
Input: nums = [-1,-1]
Output: [0,0]

Constraints:

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
    vector<int> countSmaller(vector<int>& nums) {
        vector<int> counts;
        vector<int> sorted;
        
        int len = nums.size();
        int left, right, mid, pos;
        
        if (len == 0)
            return counts;
        
        reverse(nums.begin(), nums.end());
        
        sorted.push_back(nums[0]);
        counts.push_back(0);
        
        for (int i = 1; i < len; i ++) {
            left = 0; 
            right = sorted.size();
            pos = left;
            while (left < right) {
                mid = (left + right) / 2;
                if (sorted[mid] < nums[i]) {
                    left = mid + 1;
                    pos = left;
                } else {
                    right = mid;
                    pos = right;
                }
            }
            sorted.insert(sorted.begin() + pos , nums[i]);
            counts.push_back(pos);
        }
        
        reverse(counts.begin(), counts.end());
        
        return counts;
        
    }
};




Related Posts

Super Egg Drop Problem

LeetCode 887. You are given k identical eggs and you...

Peak Index In A Mountain Array Problem

LeetCode 852. Let’s call an array arr a mountainif the...

Numbers At Most N Given Digit Set Problem

LeetCode 902. Given an array of digits which is sorted...

Nth Magical Number Problem

LeetCode 878. A positive integer is magical if it is...

Koko Eating Bananas Problem

LeetCode 875. Koko loves to eat bananas. There are n...