【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
2014-02-20 22:14
721 查看
A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper-case English letters A, C, G, T.
This string actually represents a DNA sequence, and the upper-case letters represent single nucleotides(核苷).
You are also given non-empty zero-indexed arrays P and Q consisting of M integers. These arrays represent queries about minimal nucleotides. We represent the letters of string S as integers 1, 2, 3, 4 in arrays P and Q, where A = 1, C = 2, G = 3, T = 4, and we assume that A < C < G < T.
Query K requires you to find the minimal nucleotide from the range (P[K], Q[K]), 0 ≤ P[i] ≤ Q[i] < N.
For example, consider string S = GACACCATA and arrays P, Q such that:
The minimal nucleotides from these ranges are as follows:
(0, 8) is A identified by 1,
(0, 2) is A identified by 1,
(4, 5) is C identified by 2,
(7, 7) is T identified by 4.
the function should return the values [1, 1, 2, 4], as explained above.
Assume that:
N is an integer within the range [1..100,000];
M is an integer within the range [1..50,000];
each element of array P, Q is an integer within the range [0..N − 1];
P[i] ≤ Q[i];
string S consists only of upper-case English letters A, C, G, T.
Complexity:
expected worst-case time complexity is O(N+M);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
思路:
开四个Prefix Sums数组分别用来统计ACGT从m到n的个数,如果A个数为0就看C,如此类推。
如果不是事先知道这题应该用Prefix Sums,可能没那么容易想到。
代码:
This string actually represents a DNA sequence, and the upper-case letters represent single nucleotides(核苷).
You are also given non-empty zero-indexed arrays P and Q consisting of M integers. These arrays represent queries about minimal nucleotides. We represent the letters of string S as integers 1, 2, 3, 4 in arrays P and Q, where A = 1, C = 2, G = 3, T = 4, and we assume that A < C < G < T.
Query K requires you to find the minimal nucleotide from the range (P[K], Q[K]), 0 ≤ P[i] ≤ Q[i] < N.
For example, consider string S = GACACCATA and arrays P, Q such that:
P[0] = 0 Q[0] = 8 P[1] = 0 Q[1] = 2 P[2] = 4 Q[2] = 5 P[3] = 7 Q[3] = 7
The minimal nucleotides from these ranges are as follows:
(0, 8) is A identified by 1,
(0, 2) is A identified by 1,
(4, 5) is C identified by 2,
(7, 7) is T identified by 4.
the function should return the values [1, 1, 2, 4], as explained above.
Assume that:
N is an integer within the range [1..100,000];
M is an integer within the range [1..50,000];
each element of array P, Q is an integer within the range [0..N − 1];
P[i] ≤ Q[i];
string S consists only of upper-case English letters A, C, G, T.
Complexity:
expected worst-case time complexity is O(N+M);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
思路:
开四个Prefix Sums数组分别用来统计ACGT从m到n的个数,如果A个数为0就看C,如此类推。
如果不是事先知道这题应该用Prefix Sums,可能没那么容易想到。
代码:
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) { int n = S.length(); vector<vector<int> > vACGT(4, vector<int>(1,0)); int count[4] = {0,0,0,0}; for(int i = 0; i < n; i++){ switch(S[i]){ case 'A': count[0] += 1; break; case 'C': count[1] += 1; break; case 'G': count[2] += 1; break; case 'T': count[3] += 1; break; } for(int k = 0; k < 4; k++){ vACGT[k].push_back(count[k]); } } vector<int> vres; for(int i = 0; i < P.size(); i++){ for(int k = 0; k < 4; k++){ if(vACGT[k][Q[i]+1]-vACGT[k][P[i]] > 0){ vres.push_back(k+1); break; } } } return vres; }
相关文章推荐
- 【题解】【数组】【Prefix Sums】【Codility】Passing Cars
- codility: GenomicRangeQuery
- Codility---GenomicRangeQuery
- [codility]GenomicRangeQuery
- Codility -- Genomic-range-query
- [codility]Genomic-range-query
- [codility]GenomicRangeQuery
- [codility]GenomicRangeQuery
- *[codility]GenomicRangeQuery
- codility GenomicRangeQuery
- [codility]GenomicRangeQuery
- [codility]GenomicRangeQuery
- leetcode 307. Range Sum Query - Mutable(树状数组)
- leetcode题解:Search for a Range (已排序数组范围查找)
- Colidity--GenomicRangeQuery
- LeetCode 307 Range Sum Query - Mutable(树状数组)
- GenomicRangeQuery
- (LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
- 【leetcode题解】[E][52]303. Range Sum Query - Immutable
- LeetCode题解:Range Sum Query - Immutable(C++版本)