GenomicRangeQuery

Find the minimal nucleotide from a range of sequence DNA.


Solution:

Solution to Codility's Genomic Range Query problem which is from the Codility Lesson 5: Prefix Sums and, is solved in Java 8 with 62% performance and correctness scores. The goal here is to find the minimal nucleotide from a range of sequence dna. You can find the question of this GenomicRangeQuery problem in the Codility website.


package Codility.Lesson5;

import java.util.*;

public class GenomicRangeQuery {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String s = "CAGCCTA";
		int[] in1 = {2, 5, 0};
		int[] in2 = {4, 5, 6};
		int[] result = solution(s, in1, in2);
		System.out.println(result);
	}

    public static int[] solution(String S, int[] P, int[] Q) {
        int[] r = new int[Q.length];
        // write your code in Java SE 8
        HashMap<String, Integer> dna = new HashMap<String, Integer>();
        dna.put("A", 1);
        dna.put("C", 2);
        dna.put("G", 3);
        dna.put("T", 4);

        for (int i = 0; i<P.length; i++){
            int min = Integer.MAX_VALUE;
            for (int j = P[i]; j<=Q[i]; j++){
                
                System.out.println("j:" + j);
                char s =S.charAt(j);
                String str =String.valueOf(s);
                int curVal = dna.get(str);
                if (min> curVal){
                    min = curVal;
                }
            }
            r[i] = min;
        }
        return r;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *