Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a064222

Browse files
solved 2121. Intervals Between Identical Elements
See: Why: How: Tags:
1 parent 9874eb4 commit a064222

File tree

1 file changed

+66
-0
lines changed
  • LeetCode/2121._Intervals_Between_Identical_Elements

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
#include <unordered_map>
4+
using namespace std;
5+
6+
//// START
7+
/*
8+
## 2121. Intervals Between Identical Elements
9+
10+
*/
11+
12+
class Solution {
13+
public:
14+
vector<long long> getDistances(vector<int> &arr) {
15+
unordered_map<int, vector<int>> mm;
16+
for (int i = 0; i < arr.size(); i++) {
17+
mm[arr[i]].push_back(i);
18+
}
19+
vector<long long> rets(arr.size());
20+
for (auto m : mm) {
21+
vector<long long> leftsum(m.second.size());
22+
int count = 1;
23+
for (int i = 1; i < m.second.size(); i++) {
24+
int delta = m.second[i] - m.second[i - 1];
25+
leftsum[i] = leftsum[i - 1] + count * delta;
26+
count++;
27+
}
28+
rets[m.second.back()] = leftsum.back();
29+
30+
vector<long long> rightsum(m.second.size());
31+
count = 1;
32+
for (int i = m.second.size() - 2; i >= 0; i--) {
33+
int delta = m.second[i + 1] - m.second[i];
34+
rightsum[i] = rightsum[i + 1] + count * delta;
35+
count++;
36+
rets[m.second[i]] = leftsum[i] + rightsum[i];
37+
}
38+
}
39+
return rets;
40+
}
41+
};
42+
43+
//// END
44+
struct T {};
45+
46+
TEST(Solution, test) {
47+
T ts[] = {
48+
{
49+
50+
},
51+
{
52+
53+
},
54+
55+
};
56+
57+
for (T t : ts) {
58+
Solution solution;
59+
}
60+
}
61+
62+
int main() {
63+
testing::InitGoogleTest();
64+
65+
return RUN_ALL_TESTS();
66+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /