1 2 3 4 5 6 7 8 9 |
def solution(S, P, Q): values = {"A":1,"C":2,"G":3,"T":4} ans = [] for i, j in zip(P, Q): for v in values: if v in S[i:j+1]: ans.append(values[v]) break return ans |

It’s the exact same principle as the examples above, I just tried not to exceed in the if else repetition, but it does exactly the same thing.

On the worst case cenario, it is indeed O(N*M) since python uses linear search. If all the characters of the DNA were “T”, on the lasr position, it would be N*M, but since the avarege is lower than N*M it gets as N+M

The answer is certanly in the difference between A and B ([A,B]), but we can’t rely on this difference alone since we may have cases where the same difference for the same K will give us 2 different answers. Rachel gave us a good example: A=20, B=30 and K=10 should return 2, with a difference of 10 between A and B. Although, A=21, B=31 and K=10 should return 1 despite the fact that [A,B] is still 10 and K haven’t changed.

So, let’s think a bit: if we take the rest of the division of A by K (A%K) out of A (A – A%K), we will have the closest number to A that is still divisible by K (mK). And if we take mK out of B (B-mK) we will always have the same interval between 0 and B-mK ([0, B-mK]) if two differents A and B have the same interval. Now we can count the multiples of K inside [0, B-mK] by dividing (B-mk) by K ((B-mK)/K), just like you would do to find out which number multiplied by 3 would give you 27 (27/3), althogh you have to remember that 0 still counts as divisible by K no matter what the value of K is, so we have to add this value by making it (B-mK)/K + 1.

But, remember that if A%B>0, you have a multiple of K that shouldn’t be there. In that case, mK just doesn’t exist, insted of A%B==0 where mK and A are the same (mK == A). So in A%B>0 it will be (B-mK)/K + 1 – 1 which is the same as (B-mK)/K.

Now, remember that mk is A – A%K, which can make the formula become (B-A)/K or (B-(A – A%K))/K + 1 depending on whether A%K is greater or equal to zero, leading to Sheng’s code:

1 2 3 |
def solution(A, B, K): if A % K == 0: return (B - A) // K + 1 else: return (B - (A - A % K )) // K |

Note that we we use floor division (//) so our answer will be an integer and we don’t depend on B – (A – A % K ) being a multiple of K.

But in python you can actully make it even simpler since in math operations python sees True and False as 1 and 0, and A – A % K is equal A if A % K == 0, and that leads to my code:

1 2 |
def solution(A, B, K): return (B-(A - A%K))//K + (A%K == 0) |

1 2 3 4 5 6 7 8 |
def solution(A): i = 0 xor = 0 for n in A: i += 1 xor ^= i ^ n return 1 if xor == 0 else 0 |

1 2 3 |
def solution(X, A): a = list(dict.fromkeys(A)) return -1 if len(a) != X else A.index(a[-1]) |

Very very elegant

]]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def solution(X, A): x = list(range(1, X + 1)) sum_x = sum(x) i = 0 for n in A: if x[n-1] ^ n == 0: x[n-1] = 0 sum_x -= n if sum_x == 0: return i i += 1 return -1 |

1 2 3 4 5 6 7 8 9 10 11 12 |
def solution(X, A): x = list(range(1, X + 1)) sum_x = sum(x) i = -1 while(len(A) > 0 and sum_x != 0): i += 1 n = A[i] if x[n-1] ^ n == 0: x[n-1] = 0 sum_x -= n return i |

1 2 3 4 5 6 7 |
def solution(A): if A == []: return 1 A = sorted(A) s = ((len(A) + 1) * ((len(A) + 1) + 1)) // 2 return s - sum(A) |

from [1, 2], we can only create ONE rope (as 1 + 2 = 3), whose length is equal to K (3)

]]>Actual: 1

Expected: 2

Please explain ? ]]>