-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_week_380.cpp
95 lines (86 loc) · 1.83 KB
/
leetcode_week_380.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<vector>
#include<algorithm>
#include<utility>
#include<cassert>
#include<numeric>
#include<iostream>
namespace my
{
template<typename A, typename B>
struct Map
{
constexpr auto has(A x)
{
if(std::find(keys.begin(), keys.end(), x) != std::end(keys))
{
return true;
}
else
{
return false;
}
}
constexpr auto operator[](A x)
{
for(int i = 0; i < keys.size(); i++)
{
if(keys[i] == x)
{
return values[i];
}
}
if(!has(x))
{
return A{};
}
}
auto at(int i)
{
return values[i];
}
auto append(std::pair<A, B> p)
{
keys.push_back(p.first);
values.push_back(p.second);
}
auto value_assign(A key, B x)
{
for(int i = 0; i < keys.size(); i++)
{
if(keys[i] == key)
{
values[i] = x;
}
}
}
auto accumulate_values()
{
return std::accumulate(values.begin(), values.end(), 0);
}
Map() {}
std::vector<A> keys{};
std::vector<B> values{};
};
};
auto test(std::vector<int> v) -> int // occurences
{
my::Map<int, int> m{};
int i = 0;
for(const auto x : v)
{
if(m.has(x))
{
m.value_assign(x, m.at(i) + 1);
}
else
{
m.append(std::pair<int, int>{x, 1});
}
i++;
}
return m.accumulate_values();
}
int main()
{
std::cout << (test(std::vector<int>{1,2,3,4,5})); // should print 5
}