java codility training Genomic-range-query

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

    Here is my solution Using Segment Tree O(n)+O(log n)+O(M) time

    public class DNAseq {
    
    
    public static void main(String[] args) {
        String S="CAGCCTA";
        int[] P={2, 5, 0};
        int[] Q={4, 5, 6};
        int [] results=solution(S,P,Q);
        System.out.println(results[0]);
    }
    
    static class segmentNode{
        int l;
        int r;
        int min;
        segmentNode left;
        segmentNode right;
    }
    
    
    
    public static segmentNode buildTree(int[] arr,int l,int r){
        if(l==r){
            segmentNode n=new segmentNode();
            n.l=l;
            n.r=r;
            n.min=arr[l];
            return n;
        }
        int mid=l+(r-l)/2;
        segmentNode le=buildTree(arr,l,mid);
        segmentNode re=buildTree(arr,mid+1,r);
        segmentNode root=new segmentNode();
        root.left=le;
        root.right=re;
        root.l=le.l;
        root.r=re.r;
    
        root.min=Math.min(le.min,re.min);
    
        return root;
    }
    
    public static int getMin(segmentNode root,int l,int r){
        if(root.l>r || root.r=l&& root.r<=r) {
            return root.min;
        }
        return Math.min(getMin(root.left,l,r),getMin(root.right,l,r));
    }
    public static int[] solution(String S, int[] P, int[] Q) {
        int[] arr=new int[S.length()];
        for(int i=0;i

提交回复
热议问题