traning center [Genomic Range Query]
A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and Thave impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6
The answers to these M = 3 queries are as follows:
- The part of the DNA between positions 2 and 4 contains nucleotides Gand C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
- The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
- The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide A whose impact factor is 1, so the answer is 1.
Write a function:
class Solution { public int[] solution(String S, int[] P, int[] Q); }
that, given a non-empty zero-indexed string S consisting of N characters and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
The sequence should be returned as:
- a Results structure (in C), or
- a vector of integers (in C++), or
- a Results record (in Pascal), or
- an array of integers (in any other programming language).
For example, given the string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6
the function should return the values [2, 4, 1], 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 arrays P, Q is an integer within the range [0..N − 1];
- P[K] ≤ Q[K], where 0 ≤ K < M;
- 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).
처음에는 contain을 통해 배열 안의 내용을 확인 하는 것이 시간복잡도에 영향을 줄 것이라고 생각을 못했었다. 그래서 contain을 사용해서 너무 쉬운데?? 하면서 제출을 했는데 결과는 timeout이 나오면서 66%가 되었다.
다시 코드로 돌아와 처음부터 String을 숫자로 바꾸어 계산해야겠다고 생각을 했다. 2차원 배열을 만들까 하다가, 2차원 배열은 왠지 공간복잡도 N*M으로 나올 것 같아서 한참을 헤메다 보니, 2차원 배열의 공간 복잡도는 N이었다. 그래서 배열을 통해 해당 index까지 몇 개가 나왔는지 세어주는 index라는 배열을 만들어 주었고, 연산을 해서 출력했다.
처음에는 String을 변환 시키는 과정에서 switch case를 사용하다가 enum을 사용해 보고 싶어 enum을 만들어서 작성했다.
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 | public enum Index { A(1), C(2), G(3), T(4); int value; Index(int value ) { this.value = value; } int getValue() { return this.value; } static int getValue(char value) { return valueOf(String.valueOf(value)).getValue(); } } public int[] solution(String S, int[] P, int[] Q) { int[][] index = new int[S.length()][4]; for ( int i = 0; i < S.length() ; i ++) { index[i][Index.getValue(S.charAt(i)) - 1] = 1; } for ( int i = 1; i < S.length(); i ++) { for ( int j = 0; j < 4; j ++) { index[i][j] += index[i - 1][j]; } } int[] result = new int[P.length]; for ( int i = 0; i < P.length ; i ++) { if (P[i] == 0) { for ( int j = 0; j < 4; j ++) { if (index[Q[i]][j] > 0) { result[i] = j + 1; break; } } } else { for ( int j = 0; j < 4; j ++) { if (index[Q[i]][j] - index[P[i] - 1][j] > 0) { result[i] = j + 1; break; } } } } return result; } | cs |
'--이전중입니다-- > 알고리즘' 카테고리의 다른 글
[LeetCode] 73. Set Matrix Zeroes (2) | 2019.08.03 |
---|---|
codility - MinAvgTwoSlice ( java, javascript) (0) | 2018.01.05 |
codility - MinAvgTwoSlice ( javascript ) (0) | 2017.12.31 |