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

Data Stream As Disjoint Intervals Problem

LeetCode 352. Given a data stream input of non-negative integers...

Count Of Smaller Numbers After Self Problem

LeetCode 315. You are given an integer array nums and...

Count Complete Tree Nodes Problem

LeetCode 222. Given the root of a complete binary tree,...

Find Peak Element Problem

LeetCode 162. A peak element is an element that is...

Find Minimum In Rotated Sorted Array Problem

LeetCode 153. Suppose an array of length n sorted in...

Find Minimum In Rotated Sorted Array II Problem

LeetCode 154. Suppose an array of length n sorted in...