-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenomicRangeQuery.cpp
43 lines (40 loc) · 1.15 KB
/
GenomicRangeQuery.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
// Accessible from https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected Time Complexity: O(N + M)
//
int value (char s) {
switch (s) {
case 'A': return 0;
case 'C': return 1;
case 'G': return 2;
default: return 3;
}
}
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
unsigned int n = S.size();
vector<vector<int>> sums;
for (int i = 0; i < 4; ++i) { //4 different values
sums.push_back(vector<int>(n+1,0));
}
for (unsigned int i = 1; i <= n; ++i) { //prefix sums
for (int j = 0; j < 4; ++j) {
sums[j][i] = sums[j][i-1] + (value(S[i-1]) == j ? 1 : 0);
}
}
vector<int> result;
n = P.size();
for (unsigned int i = 0; i < n; ++i) {
int from = P[i];
int to = Q[i] + 1; //+1 to account for to == from
for (int j = 0; j < 4; ++j) {
if (sums[j][to]-sums[j][from]!=0) {
result.push_back(j+1); //add 1 to index to get value
break;
}
}
}
return result;
}