java codility training Genomic-range-query

后端 未结 30 2271
悲哀的现实
悲哀的现实 2021-02-01 12:47

The task is:

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.

<
30条回答
  •  -上瘾入骨i
    2021-02-01 13:30

    Here is the solution, supposing someone is still interested.

    class Solution {
            public int[] solution(String S, int[] P, int[] Q) {
                int[] answer = new int[P.length];
                char[] chars = S.toCharArray();
                int[][] cumulativeAnswers = new int[4][chars.length + 1];
    
                for (int iii = 0; iii < chars.length; iii++) {
                    if (iii > 0) {
                        for (int zzz = 0; zzz < 4; zzz++) {
                            cumulativeAnswers[zzz][iii + 1] = cumulativeAnswers[zzz][iii];
                        }
                    }
    
                    switch (chars[iii]) {
                        case 'A':
                            cumulativeAnswers[0][iii + 1]++;
                            break;
                        case 'C':
                            cumulativeAnswers[1][iii + 1]++;
                            break;
                        case 'G':
                            cumulativeAnswers[2][iii + 1]++;
                            break;
                        case 'T':
                            cumulativeAnswers[3][iii + 1]++;
                            break;
                    }
                }
    
                for (int iii = 0; iii < P.length; iii++) {
                    for (int zzz = 0; zzz < 4; zzz++) {
    
                        if ((cumulativeAnswers[zzz][Q[iii] + 1] - cumulativeAnswers[zzz][P[iii]]) > 0) {
                            answer[iii] = zzz + 1;
                            break;
                        }
    
                    }
                }
    
                return answer;
            }
        }
    

提交回复
热议问题