java codility training Genomic-range-query

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

    pshemek's solution constrains itself to the space complexity (O(N)) - even with the 2-d array and the answer array because a constant (4) is used for the 2-d array. That solution also fits in with the computational complexity - whereas mine is O (N^2) - though the actual computational complexity is much lower because it skips over entire ranges that include minimal values.

    I gave it a try - but mine ends up using more space - but makes more intuitive sense to me (C#):

    public static int[] solution(String S, int[] P, int[] Q)
    {
        const int MinValue = 1;
        Dictionary stringValueTable = new Dictionary(){ {'A', 1}, {'C', 2}, {'G', 3}, {'T', 4} };
    
        char[] inputArray = S.ToCharArray();
        int[,] minRangeTable = new int[S.Length, S.Length]; // The meaning of this table is [x, y] where x is the start index and y is the end index and the value is the min range - if 0 then it is the min range (whatever that is)
        for (int startIndex = 0; startIndex < S.Length; ++startIndex)
        {
            int currentMinValue = 4;
            int minValueIndex = -1;
            for (int endIndex = startIndex; (endIndex < S.Length) && (minValueIndex == -1); ++endIndex)
            {
                int currentValue = stringValueTable[inputArray[endIndex]];
                if (currentValue < currentMinValue)
                {
                    currentMinValue = currentValue;
                    if (currentMinValue == MinValue) // We can stop iterating - because anything with this index in its range will always be minimal
                        minValueIndex = endIndex;
                    else
                        minRangeTable[startIndex, endIndex] = currentValue;
                }
                else
                    minRangeTable[startIndex, endIndex] = currentValue;
            }
    
            if (minValueIndex != -1) // Skip over this index - since it is minimal
                startIndex = minValueIndex; // We would have a "+ 1" here - but the "auto-increment" in the for statement will get us past this index
        }
    
        int[] result = new int[P.Length];
        for (int outputIndex = 0; outputIndex < result.Length; ++outputIndex)
        {
            result[outputIndex] = minRangeTable[P[outputIndex], Q[outputIndex]];
            if (result[outputIndex] == 0) // We could avoid this if we initialized our 2-d array with 1's
                result[outputIndex] = 1;
        }
    
        return result;
    }
    

    In pshemek's answer - the "trick" in the second loop is simply that once you've determined you've found a range with the minimal value - you don't need to continue iterating. Not sure if that helps.

提交回复
热议问题