java codility training Genomic-range-query

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

    In ruby (100/100)

    def interval_sum x,y,p
        p[y+1] - p[x]
    end
    
    def solution(s,p,q)
    
        #Hash of arrays with prefix sums
    
        p_sums = {}
        respuesta = []
    
    
        %w(A C G T).each do |letter|
            p_sums[letter] = Array.new s.size+1, 0
        end 
    
        (0...s.size).each do |count|
            %w(A C G T).each do |letter|
                p_sums[letter][count+1] = p_sums[letter][count] 
            end if count > 0
    
            case s[count]
            when 'A'
                p_sums['A'][count+1] += 1
            when 'C'
                p_sums['C'][count+1] += 1
            when 'G'
                p_sums['G'][count+1] += 1
            when 'T'
                p_sums['T'][count+1] += 1
            end 
    
        end
    
    
    
    
        (0...p.size).each do |count|
    
    
            x = p[count]
            y = q[count]
    
    
            if interval_sum(x, y, p_sums['A']) > 0 then
                respuesta << 1
                next
            end 
    
            if interval_sum(x, y, p_sums['C']) > 0 then
                respuesta << 2
                next
            end 
    
            if interval_sum(x, y, p_sums['G']) > 0 then
                respuesta << 3
                next
            end 
    
            if interval_sum(x, y, p_sums['T']) > 0 then
                respuesta << 4
                next
            end 
    
        end
    
        respuesta
    
    end
    

提交回复
热议问题