본문 바로가기

공부하자/Codility

[Codility] Lesson5. GenomicRangeQuery (C#)

문제: app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/

 

GenomicRangeQuery coding task - Learn to Code - Codility

Find the minimal nucleotide from a range of sequence DNA.

app.codility.com

 

 

            int[] arrayA = new int[S.Length + 1];
            int[] arrayC = new int[S.Length + 1];
            int[] arrayG = new int[S.Length + 1];
            int a = 0;
            int c = 0;
            int g = 0;
            int[] result = new int[P.Length];

            for (int i = 0; i < S.Length; i++)
            {
                arrayA[i + 1] = a;
                arrayC[i + 1] = c;
                arrayG[i + 1] = g;

                switch (S.Substring(i, 1))
                {
                    case "A":
                        a = a + 1;
                        arrayA[i + 1] = a;
                        break;
                    case "C":
                        c = c + 1;
                        arrayC[i + 1] = c;
                        break;
                    case "G":
                        g = g + 1;
                        arrayG[i + 1] = g;
                        break;
                }
            }

            for (int i = 0; i < P.Length; i++)
            {
                if (arrayA[P[i]] < arrayA[Q[i] + 1])
                    result[i] = 1;
                else if (arrayC[P[i]] < arrayC[Q[i] + 1])
                    result[i] = 2;
                else if (arrayG[P[i]] < arrayG[Q[i] + 1])
                    result[i] = 3;
                else
                    result[i] = 4;
            }
            return result;