-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodility_L5(Prefix Sums)_GenomicRangeQuery.cpp
More file actions
63 lines (55 loc) · 1.19 KB
/
Codility_L5(Prefix Sums)_GenomicRangeQuery.cpp
File metadata and controls
63 lines (55 loc) · 1.19 KB
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
#include <vector>
#include <string>
// Detected time complexity: O(N + M)
// Setting up the "database": count the occurencies for each impact factor at each state.
// Using the "database" we can do each querie in constant time.
std::vector<int> solution(std::string& S, std::vector<int>& P, std::vector<int>& Q)
{
enum class impact : int
{
A = 1,
C = 2,
G = 3,
T = 4
};
struct counters
{
int A = 0;
int C = 0;
int G = 0;
};
std::vector<counters> sums(S.size() + 1);
counters c;
for (size_t i = 0; i != S.size(); ++i)
{
sums[i] = c;
switch (S[i])
{
case 'A':
++c.A;
break;
case 'C':
++c.C;
break;
case 'G':
++c.G;
break;
}
}
sums.back() = c;
std::vector<int> result(P.size());
for (size_t i = 0; i != P.size(); ++i)
{
auto from = P[i];
auto to = Q[i] + 1;
if (sums[to].A - sums[from].A > 0)
result[i] = static_cast<int>(impact::A);
else if (sums[to].C - sums[from].C > 0)
result[i] = static_cast<int>(impact::C);
else if (sums[to].G - sums[from].G > 0)
result[i] = static_cast<int>(impact::G);
else
result[i] = static_cast<int>(impact::T);
}
return result;
}