GenomicRangeQuery【★★★★★】

 1         /// <summary>
 2         /// Solution - Prefix Sums
 3         /// 100/100
 4         /// </summary>
 5         /// <param name="S"></param>
 6         /// <param name="P"></param>
 7         /// <param name="Q"></param>
 8         /// <returns></returns>
 9         public static int[] solution(string S, int[] P, int[] Q)
10         {
11             int length = S.Length;
12             int[] result = new int[P.Length];
13             int[,] arr = new int[length, 4];
14 
15             for (int i = 0; i < length; i++)
16             {
17                 if (S[i] == 'A') arr[i, 0] = 1;
18                 if (S[i] == 'C') arr[i, 1] = 1;
19                 if (S[i] == 'G') arr[i, 2] = 1;
20                 if (S[i] == 'T') arr[i, 3] = 1;
21             }
22 
23             //compute prefixes
24             for (int i = 1; i < length; i++)
25             {
26                 for (int j = 0; j < 4; j++)
27                 {
28                     arr[i, j] += arr[i - 1, j];
29                 }
30             }
31 
32             for (int i = 0; i < P.Length; i++)
33             {
34                 int x = P[i];
35                 int y = Q[i];
36                 for (int j = 0; j < 4; j++)
37                 {
38                     int sub = 0;
39                     if (x - 1 >= 0)
40                         sub = arr[x - 1, j];
41                     if (arr[y, j] - sub > 0)
42                     {
43                         result[i] = j + 1;
44                         break;
45                     }
46                 }
47             }
48 
49             return result;
50         }

 

posted @ 2015-07-25 11:35  叫我霍啊啊啊  阅读(157)  评论(0编辑  收藏  举报