Skip to content

Commit b7e69ed

Browse files
committed
🚀 23-Sep-2020
1 parent f8c9377 commit b7e69ed

3 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
2+
3+
Return the answer in an array.
4+
5+
6+
7+
Example 1:
8+
9+
Input: nums = [8,1,2,2,3]
10+
Output: [4,0,1,1,3]
11+
Explanation:
12+
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
13+
For nums[1]=1 does not exist any smaller number than it.
14+
For nums[2]=2 there exist one smaller number than it (1).
15+
For nums[3]=2 there exist one smaller number than it (1).
16+
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
17+
Example 2:
18+
19+
Input: nums = [6,5,4,8]
20+
Output: [2,1,0,3]
21+
Example 3:
22+
23+
Input: nums = [7,7,7,7]
24+
Output: [0,0,0,0]
25+
26+
27+
Constraints:
28+
29+
2 <= nums.length <= 500
30+
0 <= nums[i] <= 100
31+
32+
33+
34+
35+
// Brute Force
36+
37+
class Solution {
38+
public:
39+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
40+
vector<int> res;
41+
42+
int n=nums.size();
43+
44+
for(int i=0;i<n;i++){
45+
int cnt=0;
46+
for(int j=0;j<n;j++){
47+
if(i!=j && nums[j]<nums[i])
48+
cnt++;
49+
}
50+
res.push_back(cnt);
51+
}
52+
53+
return res;
54+
}
55+
};
56+
57+
58+
59+
60+
// Optimized
61+
// Store the count in a bucket and take the running sum.
62+
63+
class Solution {
64+
public:
65+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
66+
vector<int> res;
67+
vector<int> freq(101, 0);
68+
69+
int n=nums.size();
70+
71+
for(auto &x :nums)
72+
freq[x]++;
73+
74+
for(int i=1;i<101;i++){
75+
freq[i]+=freq[i-1];
76+
}
77+
78+
for(int i=0;i<n;i++){
79+
if(nums[i]==0){
80+
res.push_back(0);
81+
} else {
82+
res.push_back(freq[nums[i]-1]);
83+
}
84+
}
85+
86+
return res;
87+
}
88+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Given two arrays of integers nums and index. Your task is to create target array under the following rules:
2+
3+
Initially target array is empty.
4+
From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
5+
Repeat the previous step until there are no elements to read in nums and index.
6+
Return the target array.
7+
8+
It is guaranteed that the insertion operations will be valid.
9+
10+
11+
12+
Example 1:
13+
14+
Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
15+
Output: [0,4,1,3,2]
16+
Explanation:
17+
nums index target
18+
0 0 [0]
19+
1 1 [0,1]
20+
2 2 [0,1,2]
21+
3 2 [0,1,3,2]
22+
4 1 [0,4,1,3,2]
23+
Example 2:
24+
25+
Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
26+
Output: [0,1,2,3,4]
27+
Explanation:
28+
nums index target
29+
1 0 [1]
30+
2 1 [1,2]
31+
3 2 [1,2,3]
32+
4 3 [1,2,3,4]
33+
0 0 [0,1,2,3,4]
34+
Example 3:
35+
36+
Input: nums = [1], index = [0]
37+
Output: [1]
38+
39+
40+
Constraints:
41+
42+
1 <= nums.length, index.length <= 100
43+
nums.length == index.length
44+
0 <= nums[i] <= 100
45+
0 <= index[i] <= i
46+
47+
48+
49+
50+
51+
52+
class Solution {
53+
public:
54+
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
55+
vector<int> res;
56+
57+
int n=nums.size();
58+
59+
for(int i=0;i<n;i++){
60+
res.insert(res.begin()+index[i], nums[i]);
61+
}
62+
63+
return res;
64+
}
65+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Given an array of integers nums.
2+
3+
A pair (i,j) is called good if nums[i] == nums[j] and i < j.
4+
5+
Return the number of good pairs.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [1,2,3,1,1,3]
12+
Output: 4
13+
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
14+
Example 2:
15+
16+
Input: nums = [1,1,1,1]
17+
Output: 6
18+
Explanation: Each pair in the array are good.
19+
Example 3:
20+
21+
Input: nums = [1,2,3]
22+
Output: 0
23+
24+
25+
Constraints:
26+
27+
1 <= nums.length <= 100
28+
1 <= nums[i] <= 100
29+
30+
31+
32+
33+
34+
35+
class Solution {
36+
public:
37+
int numIdenticalPairs(vector<int>& nums) {
38+
unordered_map<int, int> m;
39+
for(auto &num: nums){
40+
m[num]++;
41+
}
42+
43+
int res=0;
44+
45+
for(auto [num, freq]: m){
46+
if(freq>1){
47+
res+=(freq*(freq-1))/2;
48+
}
49+
}
50+
51+
return res;
52+
}
53+
};
54+
55+
56+
57+
//We can just count each value. Then, n elements with the same value can form n * (n - 1) / 2 pairs.
58+
59+

0 commit comments

Comments
 (0)