java codility training Genomic-range-query

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

    perl 100/100 solution:

    sub solution {
        my ($S, $P, $Q)=@_; my @P=@$P; my @Q=@$Q;
    
        my @_A = (0), @_C = (0), @_G = (0), @ret =();
        foreach (split //, $S)
        {
            push @_A, $_A[-1] + ($_ eq 'A' ? 1 : 0);
            push @_C, $_C[-1] + ($_ eq 'C' ? 1 : 0);
            push @_G, $_G[-1] + ($_ eq 'G' ? 1 : 0);
        }
    
        foreach my $i (0..$#P)
        {
            my $from_index = $P[$i];
            my $to_index = $Q[$i] + 1;
            if ( $_A[$to_index] - $_A[$from_index] > 0 )
            {
                push @ret, 1;
                next;
            }
            if ( $_C[$to_index] - $_C[$from_index] > 0 )
            {
                push @ret, 2;
                next;
            }
            if ( $_G[$to_index] - $_G[$from_index] > 0 )
            {
                push @ret, 3;
                next;
            }
            push @ret, 4
        }
    
        return @ret;
    }
    

提交回复
热议问题