java codility training Genomic-range-query

后端 未结 30 2664
悲哀的现实
悲哀的现实 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条回答
  •  时光取名叫无心
    2021-02-01 13:43

    Java 100/100

    class Solution {
    public int[] solution(String S, int[] P, int[] Q) {
        int     qSize       = Q.length;
        int[]   answers     = new int[qSize];
    
        char[]  sequence    = S.toCharArray();
        int[][] occCount    = new int[3][sequence.length+1];
    
        int[] geneImpactMap = new int['G'+1];
        geneImpactMap['A'] = 0;
        geneImpactMap['C'] = 1;
        geneImpactMap['G'] = 2;
    
        if(sequence[0] != 'T') {
            occCount[geneImpactMap[sequence[0]]][0]++;
        }
    
        for(int i = 0; i < sequence.length; i++) {
            occCount[0][i+1] = occCount[0][i];
            occCount[1][i+1] = occCount[1][i];
            occCount[2][i+1] = occCount[2][i];
    
            if(sequence[i] != 'T') {
                occCount[geneImpactMap[sequence[i]]][i+1]++;
            }
        }
    
        for(int j = 0; j < qSize; j++) {
            for(int k = 0; k < 3; k++) {
                if(occCount[k][Q[j]+1] - occCount[k][P[j]] > 0) {
                    answers[j] = k+1;
                    break;
                }
    
                answers[j] = 4;
            }            
        }
    
        return answers;
    }
    } 
    

提交回复
热议问题