-
Notifications
You must be signed in to change notification settings - Fork 25
/
GenomicRangeQuery.cpp
150 lines (130 loc) · 3.27 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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)
//
#include <vector>
#include <string>
using namespace std;
#include <vector>
#include <string>
using namespace std;
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
const int A_VAL = 1;
const int C_VAL = 2;
const int G_VAL = 3;
const int T_VAL = 4;
// Have to track these values separately, this costs us space complexity
// but saves us time complexity. Can't do this simply with prefix_sums with
// single vector of fundamental type (or at least I am not clever enough to see it)
int N = S.size();
vector<int> A(N, -1);
vector<int> C(N, -1);
vector<int> G(N, -1);
vector<int> T(N, -1);
int aLastIndex = -1;
int cLastIndex = -1;
int gLastIndex = -1;
int tLastIndex = -1;
// Complexity of this operation is O(N)
for (int n=0; n<N; n++) {
switch (S[n]) {
case 'A':
aLastIndex = n;
break;
case 'C':
cLastIndex = n;
break;
case 'G':
gLastIndex = n;
break;
case 'T':
tLastIndex = n;
break;
default:
break;
}
A[n] = aLastIndex;
C[n] = cLastIndex;
G[n] = gLastIndex;
T[n] = tLastIndex;
}
int M = P.size();
vector<int> result(M, 0);
// Complexity of this operation is O(M+1) ==> O(M)
for (int m=0; m<M; m++) {
int l = P[m];
int r = Q[m];
// Check if the last seen index is greater or less than the left value
// If it is then that is the nucleotide for this space. Done in order
// of lowest to highest impact factor.
// LOGIC:
// 1. A[r] cannot be greater than the n at that location.
// See assignment above, so no need to check it separately
// 2. A[r] will be equal to l if A was last seen at index l
// 3. A[r] will be less than l if A was last seen before index l
// 4. A[r] will be greater than l if A was seen after l up to and including r
if ( A[r] >= l ) {
result[m] = A_VAL;
} else if ( C[r] >= l ) {
result[m] = C_VAL;
} else if ( G[r] >= l ) {
result[m] = G_VAL;
} else if ( T[r] >= l ) {
result[m] = T_VAL;
}
}
return result;
}
// End of submitted code
//
// This code here shows the evolution of my program, from brute force
// to final draft
//
#ifdef BRUTE_FORCE
// Original Solution
//
// Task Score: 62%
// Correctness: 100%
// Performance: 0%
// Detected time complexity: Not detected, performance timed out.
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
const int A_VAL = 1;
const int C_VAL = 2;
const int G_VAL = 3;
const int T_VAL = 4;
// iterate through array and create my value array
int N = S.size();
vector<int> V(N, 0);
for (int n=0; n<N; n++) {
switch (S[n]) {
case 'A': V[n] = A_VAL; break;
case 'C': V[n] = C_VAL; break;
case 'G': V[n] = G_VAL; break;
case 'T': V[n] = T_VAL; break;
default: break;
}
}
int M = P.size();
vector<int> result(M, 0);
for (int m=0; m<M; m++) {
int l = P[m];
int r = Q[m];
int minimum = 4;
for (int i=l; i<=r; i++) {
if (V[i] < minimum) {
minimum = V[i];
}
// If this is an A then we know minimum is 1, can go to next iteration
if (minimum == 1) {
result[m] = minimum;
break;
}
}
result[m] = minimum;
}
return result;
}
#endif