Skip to content

Commit 56af2cb

Browse files
committed
day 26
1 parent df83652 commit 56af2cb

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Count of Smaller Numbers After Self
3+
===================================
4+
5+
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+
class Solution
30+
{
31+
public:
32+
vector<pair<int, int>> temp, B;
33+
34+
void crossInversion(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+
void countInversion(int st, int en, vector<int> &A)
77+
{
78+
if (st >= en)
79+
return;
80+
int mid = (st + en) / 2;
81+
82+
countInversion(st, mid, A);
83+
countInversion(mid + 1, en, A);
84+
crossInversion(st, en, A);
85+
}
86+
87+
vector<int> countSmaller(vector<int> &A)
88+
{
89+
int n = A.size();
90+
91+
temp = vector<pair<int, int>>(n, {0, 0});
92+
B = vector<pair<int, int>>(n, {0, 0});
93+
vector<int> ans(n, 0);
94+
95+
for (int i = 0; i < n; ++i)
96+
B[i] = {A[i], i};
97+
98+
countInversion(0, n - 1, ans);
99+
return ans;
100+
}
101+
};

Leetcode Daily Challenge/June-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
| 23. | [Reverse Linked List II](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/606/week-4-june-22nd-june-28th/3789/) [cpp](./23.%20Reverse%20Linked%20List%20II.cpp) |
2828
| 24. | [Out of Boundary Paths](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/606/week-4-june-22nd-june-28th/3790/) [cpp](./24.%20Out%20of%20Boundary%20Paths.cpp) |
2929
| 25. | [Redundant Connection](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/606/week-4-june-22nd-june-28th/3791/) [cpp](./25.%20Redundant%20Connection.cpp) |
30+
| 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

Comments
 (0)