java codility training Genomic-range-query

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

    Here is the solution that got 100 out of 100 in codility.com. Please read about prefix sums to understand the solution:

    public static int[] solveGenomicRange(String S, int[] P, int[] Q) {
            //used jagged array to hold the prefix sums of each A, C and G genoms
            //we don't need to get prefix sums of T, you will see why.
            int[][] genoms = new int[3][S.length()+1];
            //if the char is found in the index i, then we set it to be 1 else they are 0
            //3 short values are needed for this reason
            short a, c, g;
            for (int i=0; i 0) {
                    result[i] = 1;
                } else if (genoms[1][toIndex] - genoms[1][fromIndex] > 0) {
                    result[i] = 2;
                } else if (genoms[2][toIndex] - genoms[2][fromIndex] > 0) {
                    result[i] = 3;
                } else {
                    result[i] = 4;
                }
            }
    
            return result;
        }
    

提交回复
热议问题