You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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].
6
+
7
+
Example 1:
8
+
Input: nums = [5,2,6,1]
9
+
Output: [2,1,1,0]
10
+
Explanation:
11
+
To the right of 5 there are 2 smaller elements (2 and 1).
12
+
To the right of 2 there is only 1 smaller element (1).
13
+
To the right of 6 there is 1 smaller element (1).
14
+
To the right of 1 there is 0 smaller element.
15
+
16
+
Example 2:
17
+
Input: nums = [-1]
18
+
Output: [0]
19
+
20
+
Example 3:
21
+
Input: nums = [-1,-1]
22
+
Output: [0,0]
23
+
24
+
Constraints:
25
+
1 <= nums.length <= 105
26
+
-104 <= nums[i] <= 104
27
+
*/
28
+
29
+
classSolution
30
+
{
31
+
public:
32
+
vector<pair<int, int>> temp, B;
33
+
34
+
voidcrossInversion(int st, int en, vector<int> &A)
35
+
{
36
+
int mid = (st + en) / 2;
37
+
int i = st, j = mid + 1;
38
+
int k = st;
39
+
40
+
while (i <= mid && j <= en)
41
+
{
42
+
if (B[i].first <= B[j].first)
43
+
{
44
+
temp[k] = B[j];
45
+
j++;
46
+
k++;
47
+
}
48
+
else
49
+
{
50
+
temp[k] = B[i];
51
+
A[B[i].second] += (en - j + 1);
52
+
i++;
53
+
k++;
54
+
}
55
+
}
56
+
57
+
while (i <= mid)
58
+
{
59
+
temp[k] = B[i];
60
+
A[B[i].second] += (en - j + 1);
61
+
i++;
62
+
k++;
63
+
}
64
+
65
+
while (j <= en)
66
+
{
67
+
temp[k] = B[j];
68
+
j++;
69
+
k++;
70
+
}
71
+
72
+
for (int i = st; i <= en; ++i)
73
+
B[i] = temp[i];
74
+
}
75
+
76
+
voidcountInversion(int st, int en, vector<int> &A)
| 26. |[Count of Smaller Numbers After Self](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/606/week-4-june-22nd-june-28th/3792/)[cpp](./26.%20Count%20of%20Smaller%20Numbers%20After%20Self.cpp)|
0 commit comments