-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2670.cpp
More file actions
24 lines (23 loc) · 760 Bytes
/
2670.cpp
File metadata and controls
24 lines (23 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<int> distinctDifferenceArray(vector<int>& nums) {
vector<int> hashPrefix(51, 0);
vector<int> hashSuffix(51, 0);
int distinctPrefix = 0;
int distinctSuffix = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
hashSuffix[nums[i]]++;
if (hashSuffix[nums[i]] == 1) distinctSuffix++;
}
vector<int> res(n, 0);
for (int i = 0; i < n; ++i) {
hashPrefix[nums[i]]++;
if (hashPrefix[nums[i]] == 1) distinctPrefix++;
hashSuffix[nums[i]]--;
if (hashSuffix[nums[i]] == 0) distinctSuffix--;
res[i] = distinctPrefix - distinctSuffix;
}
return res;
}
};