259. GenomicRangeQuery

아현·2021년 8월 14일
0

Algorithm

목록 보기
271/400



1. JavaScript



// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(S, P, Q) {
    // write your code in JavaScript (Node.js 8.9.4)
    const array = Array.from(S);
    const N = array.length;
    let A = new Array(N+1).fill(0);
    let C = new Array(N+1).fill(0);
    let G = new Array(N+1).fill(0);
    let result = [];
    
    for(let i=0; i<N; i++) {
        switch(array[i]){
            case 'A':
                A[i+1]++;
                break;
            case 'C':
                C[i+1]++;
                break;
            case 'G':
                G[i+1]++;
                break;
            default:
                break;
        }
        A[i+1] += A[i];
        C[i+1] += C[i];
        G[i+1] += G[i];
    }

    for(let K in P){
        let start = P[K];
        let end = Q[K] + 1;
        
        if(A[start] != A[end]){
            result[K] = 1;
        }
        else if(C[start] != C[end]){
            result[K] = 2;
        }
        else if(G[start] != G[end]){
            result[K] = 3;
        }
        else{
            result[K] = 4;
        }
    }
    
    return result;
}



2. Python



def solution(S, P, Q):
    R = []
    for i in range(len(P)):
        A = P[i]
        B = Q[i]
        min_num = 5
        if 'A' in S[A:B+1]:
            num = 1
        elif 'C' in S[A:B+1]:
            num = 2
        elif 'G' in S[A:B+1]:
            num = 3
        elif 'T' in S[A:B+1]:
            num = 4
        if min_num > num :
            min_num = num
        
        R.append(min_num)
    return R

  • 작은 값부터 있는지 확인
profile
For the sake of someone who studies computer science

0개의 댓글