java codility training Genomic-range-query

后端 未结 30 2261
悲哀的现实
悲哀的现实 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:25

    This program has got score 100 and performance wise has got an edge over other java codes listed above!

    The code can be found here.

    public class GenomicRange {
    
    final int Index_A=0, Index_C=1, Index_G=2, Index_T=3;
    final int A=1, C=2, G=3, T=4; 
    
    public static void main(String[] args) {
    
        GenomicRange gen = new GenomicRange();
        int[] M = gen.solution( "GACACCATA", new int[] { 0,0,4,7 } , new int[] { 8,2,5,7 } );
        System.out.println(Arrays.toString(M));
    } 
    
    public int[] solution(String S, int[] P, int[] Q) {
    
        int[] M = new int[P.length];
        char[] charArr = S.toCharArray();
        int[][] occCount = new int[3][S.length()+1];
    
        int charInd = getChar(charArr[0]);
    
        if(charInd!=3) {
            occCount[charInd][1]++;
        }
    
        for(int sInd=1; sInd=occCount[0].length) continue;
    
            a =  occCount[Index_A][Q[i]+1] - occCount[Index_A][P[i]];
            c =  occCount[Index_C][Q[i]+1] - occCount[Index_C][P[i]];
            g =  occCount[Index_G][Q[i]+1] - occCount[Index_G][P[i]];
    
            M[i] = a>0? A : c>0 ? C : g>0 ? G : T;    
        }
    
        return M;
    }
    
    private int getChar(char c) {
    
        return ((c=='A') ? Index_A : ((c=='C') ? Index_C : ((c=='G') ? Index_G : Index_T)));  
    }
    }
    

提交回复
热议问题