1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Private Function solution(S As String, P As Integer(), Q As Integer()) As Integer() Dim Code As New Dictionary(Of String, Integer) Code.Add("A", 1) Code.Add("C", 2) Code.Add("G", 3) Code.Add("T", 4) Dim ImpactSum(0 to S.Length, 0 to 3) As Integer For Index as integer = 1 to S.Length For ImpactIndex as Integer = 0 to 3 ImpactSum(Index, ImpactIndex) = ImpactSum(Index - 1, ImpactIndex) Next ImpactSum(Index, Code(S.SubString(Index - 1, 1)) - 1) += 1 Next Dim MinimumImpact(0 to P.Length - 1) as Integer For QueryIndex As Integer = 0 to P.Length - 1 For ImpactIndex as Integer = 3 to 0 Step -1 Dim NumberOfInstances as Integer = ImpactSum(Q(QueryIndex) + 1, ImpactIndex) - ImpactSum(P(QueryIndex), ImpactIndex) If NumberOfInstances > 0 Then MinimumImpact(QueryIndex) = ImpactIndex + 1 Next Next return MinimumImpact End Function |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public int solution(int[] A) { if (null == A || A.length < 3) return 0; Arrays.sort(A); for (int P = 0; P < A.length - 2; P++) { for (int Q = P+1; Q < A.length - 1; Q++) { for (int R = P+2; R < A.length; R++) { if(P<Q && Q A[R]) && ((long)A[P] + A[R] > A[Q]) && ((long)A[R] + A[Q] > A[P])) { return 1; } } } } } return 0; } |

for (var number of A) ]]>

For the first solution, I explicitly said, it’s bad in the worst-case. For the second one, I tried again on Codility and it still passes the Codility test.

Finally, please read the “Guideline for Comments” for posting code. I cannot see your complete solution. With your incomplete post, I did not see much difference between your solution and mine.

]]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
def F_upto_A(L): # Fibonacci sequence up to the # length of A (include starting and destination position) F = [] F.append(0) F.append(1) while F[-1] <= L: F.append(F[-1]+F[-2]) return F[1:-1] def solution(A): # add starting position to A A.insert(0, 1) # add destination position to A A.append(1)#[1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1] n = len(A)#13 # store available fibonacci jumps F = F_upto_A(n)#[1, 1, 2, 3, 5, 8, 13] # S mapping A in position # and storing the minimum step count to every "1" position S = [n] * n S[0] = 0 #[0, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13] for i in xrange(1, n): # check if the position is 1 in A if A[i] == 1 : #loop the Fibonacci sequence for x in F: # previous position prev = i - x if prev >= 0: # (the minimum step count of the previous position) # plus # (one more step to the existing position) # if less than the step count of the existing position # update the step count of the existing position if S[prev] + 1 < S[i]: S[i] = S[prev] + 1 else: break # return the last position of S, if S[-1]==n , # means destination can'tbe reached # S:[0, 13, 13, 13, 13, 1, 13, 2, 13, 13, 13, 13, 3] return S[-1] if S[-1] < n else -1 |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
def solution(S): # write your code in Python 2.7 queue = [] for elem in S: if (elem in ["(","{","["]) : queue.append(elem) else: if (len(queue) == 0): return 0 if (elem in [")","}","]"]): elem_open = queue.pop() if (elem_open == "(" and elem == ")" or elem_open == "[" and elem == "]" or elem_open == "{" and elem == "}"): # print elem_open+"n" continue else: return 0; if len(queue) == 0: return 1 else: return 0 |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
if(count($A) == 2) { return abs($A[0] - $A[1]); } else { $head = $A[0]; $tail = 0; for($i=1; $i < count($A); $i++) { $tail += $A[$i]; } $min_diff = abs($head-$tail); for($i=1; $i < count($A)-1; $i++){ $head += $A[$i]; $tail -= $A[$i]; if(abs($head-$tail) < $min_diff) { $min_diff = abs($head-$tail); } } return $min_diff; } |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function solution($A,$k) { $shiftedArray = array_fill(0, count($A), NULL); echo count($shiftedArray); for ($i=0; $i<count($A); $i++) { if ($i+$k>=count($A)) { $shiftedArray[($i+$k)%count($A)]=$A[$i]; } else { $shiftedArray[$i+$k]=$A[$i]; } } return $shiftedArray; } |