├── 1-Iterations_cckao.pdf ├── 10-PrimeNumbers_cckao.pdf ├── 11-Sieve_cckao.pdf ├── 12-Gcd_cckao.pdf ├── 13-Fibonacci_cckao.pdf ├── 14-BinarySearch_cckao.pdf ├── 15-CaterpillarMethod_cckao.pdf ├── 16-GreedyAlgorithms_cckao.pdf ├── 17-DynamicProgramming_cckao.pdf ├── 2-Arrays_cckao.pdf ├── 3-TimeComplexity_cckao.pdf ├── 4-CountingElements_cckao.pdf ├── 5-PrefixSums_cckao.pdf ├── 6-Sorting_cckao.pdf ├── 7-Stacks_cckao.pdf ├── 8-Leader_cckao.pdf ├── 9-MaxSlice_cckao.pdf ├── AbsDistinct.py ├── BinaryGap.py ├── Brackets_v2.py ├── ChocolatesByNumbers_high_performance.py ├── ChocolatesByNumbers_low_performance.py ├── CountDistinctSlices_high_performance.py ├── CountDistinctSlices_low_performance.py ├── CountDiv_v2.py ├── CountFactors.py ├── CountNonDivisible.py ├── CountSemiprimes_high_performance.py ├── CountSemiprimes_low_performance.py ├── CountTriangles.py ├── CyclicRotation.py ├── Distinct.py ├── Dominator.py ├── EquiLeader.py ├── FibFrog_performance_50.py ├── Fish.py ├── FrogJmp.py ├── FrogRiverOne.py ├── GenomicRangeQuery_v2.py ├── Ladder_high_performance.py ├── Ladder_low_performance.py ├── MaxCounters_v3.py ├── MaxDoubleSliceSum.py ├── MaxNonoverlappingSegments.py ├── MaxProductOfThree.py ├── MaxProfit.py ├── MaxSliceSum.py ├── MinAvgTwoSlice_v2.py ├── MinMaxDivision.py ├── MinPerimeterRectangle.py ├── MissingInteger.py ├── Nesting.py ├── NumberOfDiscIntersections_v2.py ├── NumberSolitaire.py ├── OddOccurrencesInArray.py ├── PassingCars.py ├── Peaks.py ├── PermCheck.py ├── PermMissingElem.py ├── Python_Notes_Part1.docx ├── Python_Notes_part2.docx ├── Python_Useful_Functions.docx ├── README.md ├── StoneWall.py ├── TapeEquilibrium.py ├── TieRopes.py └── Triangle.py /1-Iterations_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/1-Iterations_cckao.pdf -------------------------------------------------------------------------------- /10-PrimeNumbers_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/10-PrimeNumbers_cckao.pdf -------------------------------------------------------------------------------- /11-Sieve_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/11-Sieve_cckao.pdf -------------------------------------------------------------------------------- /12-Gcd_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/12-Gcd_cckao.pdf -------------------------------------------------------------------------------- /13-Fibonacci_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/13-Fibonacci_cckao.pdf -------------------------------------------------------------------------------- /14-BinarySearch_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/14-BinarySearch_cckao.pdf -------------------------------------------------------------------------------- /15-CaterpillarMethod_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/15-CaterpillarMethod_cckao.pdf -------------------------------------------------------------------------------- /16-GreedyAlgorithms_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/16-GreedyAlgorithms_cckao.pdf -------------------------------------------------------------------------------- /17-DynamicProgramming_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/17-DynamicProgramming_cckao.pdf -------------------------------------------------------------------------------- /2-Arrays_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/2-Arrays_cckao.pdf -------------------------------------------------------------------------------- /3-TimeComplexity_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/3-TimeComplexity_cckao.pdf -------------------------------------------------------------------------------- /4-CountingElements_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/4-CountingElements_cckao.pdf -------------------------------------------------------------------------------- /5-PrefixSums_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/5-PrefixSums_cckao.pdf -------------------------------------------------------------------------------- /6-Sorting_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/6-Sorting_cckao.pdf -------------------------------------------------------------------------------- /7-Stacks_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/7-Stacks_cckao.pdf -------------------------------------------------------------------------------- /8-Leader_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/8-Leader_cckao.pdf -------------------------------------------------------------------------------- /9-MaxSlice_cckao.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/9-MaxSlice_cckao.pdf -------------------------------------------------------------------------------- /AbsDistinct.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_dictionary = {} 8 | 9 | for item in A: 10 | temp = abs(item) 11 | if temp not in my_dictionary: 12 | my_dictionary[temp] = True 13 | 14 | # print(my_dictionary) 15 | #print(len(my_dictionary)) 16 | return len(my_dictionary) 17 | -------------------------------------------------------------------------------- /BinaryGap.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(N): 5 | # write your code in Python 3.6 6 | # using the "concept of bit manipulation" and "& operation" 7 | 8 | current_gap = 0 9 | max_gap = 0 10 | 11 | start_counting = False 12 | 13 | temp = N 14 | 15 | while temp > 0: 16 | 17 | # case 1 18 | if (temp & 1 == 1): 19 | # case 1-1 20 | if (start_counting == False): 21 | start_counting = True 22 | # case 1-2 23 | elif (start_counting == True): 24 | max_gap = max(max_gap, current_gap) 25 | current_gap = 0 #reset 26 | 27 | # case 2 28 | elif (temp & 1 == 0): 29 | if(start_counting == True): 30 | current_gap += 1 31 | 32 | # shift one bit (every loop) 33 | temp = temp >> 1 34 | 35 | return max_gap 36 | -------------------------------------------------------------------------------- /Brackets_v2.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(S): 5 | # write your code in Python 3.6 6 | 7 | my_stack = [] 8 | 9 | # note: use 'insert(index, item)' and 'pop(index)' 10 | 11 | for char in S: 12 | if char == '{' or char == '[' or char == '(': 13 | my_stack.insert( len(my_stack), char) 14 | 15 | # note: check if the stack is empty or not (be careful) 16 | if len(my_stack) == 0: 17 | return 0 18 | elif char == ')': 19 | pop = my_stack.pop( len(my_stack)-1 ) 20 | if pop != '(': 21 | return 0 22 | elif char == ']': 23 | pop = my_stack.pop( len(my_stack)-1 ) 24 | if pop != '[': 25 | return 0 26 | elif char == '}': 27 | pop = my_stack.pop( len(my_stack)-1 ) 28 | if pop != '{': 29 | return 0 30 | 31 | # note: check if the stack is empty or not (be careful) 32 | if len(my_stack)!=0: 33 | return 0 34 | else: 35 | return 1 36 | -------------------------------------------------------------------------------- /ChocolatesByNumbers_high_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(N, M): 5 | # write your code in Python 3.6 6 | 7 | # key: 8 | # meet in the circle = number of chocolates that you will eat 9 | # number of chocolates = N / gcd(N, M) 10 | num_chocolates = N // gcd(N,M) 11 | 12 | return num_chocolates 13 | 14 | # find gcd (greatest common divisor) 15 | def gcd(a, b): 16 | if a % b == 0: 17 | return b 18 | else: 19 | return gcd(b, a%b) 20 | -------------------------------------------------------------------------------- /ChocolatesByNumbers_low_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(N, M): 5 | # write your code in Python 3.6 6 | 7 | my_dictionary = {} 8 | 9 | current_value = 0 10 | 11 | while current_value not in my_dictionary: 12 | my_dictionary[current_value] = 1 13 | current_value = (current_value + M) % N 14 | # print(my_dictionary) 15 | 16 | return len(my_dictionary) 17 | -------------------------------------------------------------------------------- /CountDistinctSlices_high_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(M, A): 5 | # write your code in Python 3.6 6 | 7 | # try to remember which value was counted 8 | current_dictionary = {} 9 | 10 | len_A = len(A) 11 | count_slice = 0 12 | begin = 0 13 | end = 0 14 | 15 | # cbombine the two loops (using one while loop) 16 | while (begin < len_A) and (end < len_A): 17 | # distinct value (end +=1) 18 | if A[end] not in current_dictionary: 19 | current_dictionary[ A[end] ] = 1 20 | # super important: not just plus 1 (the key to be faster) 21 | count_slice += (end - begin +1) 22 | if count_slice > 1_000_000_000: 23 | return 1_000_000_000 24 | end +=1 25 | # smae value 26 | else: 27 | # the same value happens (begin +=1) 28 | # important: remove the value of begin 29 | current_dictionary.pop(A[begin]) 30 | begin+=1 31 | 32 | return count_slice 33 | -------------------------------------------------------------------------------- /CountDistinctSlices_low_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(M, A): 5 | # write your code in Python 3.6 6 | 7 | # try to remember which value was counted 8 | current_dictionary = {} 9 | 10 | len_A = len(A) 11 | count_slice = 0 12 | begin = 0 13 | end = 0 14 | 15 | for begin in range(len_A): 16 | end = begin 17 | # print(begin) 18 | while end < len_A: 19 | # print(end) 20 | if A[end] not in current_dictionary: 21 | current_dictionary[ A[end] ] = 1 22 | count_slice += 1 23 | # print('count plus 1') 24 | if count_slice > 1_000_000_000: 25 | return 1_000_000_000 26 | end +=1 27 | else: 28 | # the same value happens (begin +=1) 29 | current_dictionary = {} 30 | break 31 | 32 | # important: 33 | # begin will plus 1, and so we reset 'current_dictionary' 34 | current_dictionary = {} 35 | 36 | return count_slice 37 | -------------------------------------------------------------------------------- /CountDiv_v2.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A, B, K): 5 | # write your code in Python 3.6 6 | 7 | # need to achieve low complexity O(1) 8 | # using math equation (low complexity) 9 | 10 | # number of divisible values smaller than B 11 | num_B = B // K 12 | # note: take "Math.floor" which is the basic number 13 | 14 | # number of divisible values smaller than A 15 | num_A = A // K 16 | # note: take "Math.floor" which is the basic number 17 | 18 | # number of divisible numbers 19 | num_divisible = num_B - num_A 20 | 21 | # note: plus one (if A % K == 0) 22 | # because "A" is also divisble 23 | plus = 0 24 | if A % K ==0: 25 | plus =1 26 | 27 | num_divisible = num_divisible + plus 28 | 29 | return num_divisible 30 | -------------------------------------------------------------------------------- /CountFactors.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import math 5 | 6 | def solution(N): 7 | # write your code in Python 3.6 8 | 9 | my_dictionary = {} 10 | 11 | # be careful about the range 12 | # O(n) 13 | ''' 14 | for n in range( 1, N+1 ): 15 | if N % n == 0: 16 | my_dictionary[n] = True 17 | ''' 18 | 19 | # O( log(n) ) 20 | # be careful: we need to check 'math.sqrt(N)+1' 21 | for n in range( 1, int( math.sqrt(N) ) +1 ): 22 | if N % n ==0: 23 | my_dictionary[n] = True 24 | another_factor = int( N/n ) 25 | my_dictionary[another_factor] = True 26 | 27 | # print(my_dictionary) 28 | 29 | num_factors = len( my_dictionary ) 30 | 31 | return num_factors 32 | -------------------------------------------------------------------------------- /CountNonDivisible.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import math 5 | 6 | def solution(A): 7 | 8 | # count the elements of A (by using dictionary) 9 | my_dictionary_1 = {} 10 | for item in A: 11 | if item not in my_dictionary_1: 12 | my_dictionary_1[item] = 1 13 | else: 14 | my_dictionary_1[item] += 1 15 | # print(my_dictionary_1) 16 | 17 | # count the number of non-divisors (into dictionary_2) 18 | my_dictionary_2 = {} 19 | for item in my_dictionary_1: 20 | num_divisor = 0 21 | item_sqrt = math.floor(math.sqrt(item)) 22 | # print( str(item) + ' , ' + str(item_sqrt) ) 23 | for i in range(1, item_sqrt+1, 1): 24 | if (item % i ==0): 25 | another_divisor = item / i 26 | 27 | if i in my_dictionary_1: 28 | num_divisor = num_divisor + my_dictionary_1[i] 29 | 30 | if another_divisor != i: 31 | if another_divisor in my_dictionary_1: 32 | num_divisor = num_divisor + my_dictionary_1[another_divisor] 33 | 34 | num_non_divisor = len(A) - num_divisor 35 | my_dictionary_2[item] = num_non_divisor 36 | # print(my_dictionary_2) 37 | 38 | # put the number of non-divisors into an 'array' (by using list) 39 | array_non_divisor = [] 40 | for i in range( len(A) ): 41 | array_non_divisor.append( my_dictionary_2[A[i]] ) 42 | 43 | return array_non_divisor 44 | -------------------------------------------------------------------------------- /CountSemiprimes_high_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import math 5 | 6 | def solution(N, P, Q): 7 | # write your code in Python 3.6 8 | 9 | # 1 10 | # find primes: use 'Sieve of Eratosthenes' 11 | prime_list_boolean = [True] * (N+1) # note: plus one for "0" 12 | prime_list_boolean[0] = False 13 | prime_list_boolean[1] = False 14 | for i in range( 2, math.floor(math.sqrt(N))+1 ): 15 | if prime_list_boolean[i] == True: 16 | j = i + i 17 | for not_prime in range( j, N+1, i ): 18 | prime_list_boolean[not_prime] = False 19 | 20 | # 2 21 | # append 'prime' to list 22 | prime_list = [] 23 | for index in range( N+1 ): 24 | if prime_list_boolean[index] == True: 25 | prime_list.append(index) 26 | # print(prime_list) 27 | 28 | # 3 29 | # find semi-primes 30 | semiprime_list_boolean = [False] * (N+1) # note: plus one for "0" 31 | for i in range( len(prime_list) ): 32 | for j in range(i, len(prime_list), 1): 33 | semiprime_temp = prime_list[i] * prime_list[j] 34 | if semiprime_temp > N: 35 | break 36 | else: 37 | semiprime_list_boolean[semiprime_temp] = True 38 | 39 | # 4 40 | # count the number of semi-primes 41 | count_semiprime_list = [0] * (N+1) 42 | num_semiprime_so_far = 0 43 | for i in range(N+1): 44 | if semiprime_list_boolean[i]==True: 45 | num_semiprime_so_far += 1 46 | count_semiprime_list[i] = num_semiprime_so_far 47 | #print(count_semiprime_list) 48 | 49 | # 5 50 | # return answers to all the queries 51 | answer_list = [0] * len(P) 52 | for i in range( len(P) ): 53 | begin_value = P[i] 54 | end_value = Q[i] 55 | #print(count_semiprime_list[end_value]) 56 | #print(count_semiprime_list[begin_value]) 57 | # be very careful about the 'begin_value' (not included) 58 | answer_list[i] = count_semiprime_list[end_value] - count_semiprime_list[ (begin_value-1) ] 59 | #print(answer_list) 60 | 61 | return answer_list 62 | -------------------------------------------------------------------------------- /CountSemiprimes_low_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import math 5 | 6 | def solution(N, P, Q): 7 | # write your code in Python 3.6 8 | 9 | # 1 10 | # find primes: use 'Sieve of Eratosthenes' 11 | prime_list = [] 12 | # put all values 13 | # be careful about the range 14 | for index in range( 2, N+1 ): 15 | prime_list.append(index) 16 | #print(prime_list) 17 | 18 | # 2 19 | # remove 'not prime' 20 | # be careful about the range 21 | for item in range( 2, math.floor(math.sqrt(N))+1 ): 22 | not_prime_first = item + item 23 | for not_prime in range( not_prime_first, N+1, item ): 24 | if not_prime in prime_list: 25 | prime_list.remove(not_prime) 26 | #print(prime_list) 27 | 28 | # 3 29 | # find semi-primes 30 | semiprime_list = [] 31 | for i in range( len(prime_list) ): 32 | for j in range( i, len(prime_list) ): 33 | semiprime = prime_list[i] * prime_list[j] 34 | if semiprime <= N: 35 | semiprime_list.append(semiprime) 36 | semiprime_list.sort() 37 | #print(semiprime_list) 38 | 39 | # 4 40 | # count the number of semi-primes 41 | count_semiprime_list = [0] * (N+1) 42 | num_semiprime_so_far = 0 43 | for i in range(N+1): 44 | if i in semiprime_list: 45 | num_semiprime_so_far += 1 46 | count_semiprime_list[i] = num_semiprime_so_far 47 | #print(count_semiprime_list) 48 | 49 | # 5 50 | # return answers to all the queries 51 | answer_list = [0] * len(P) 52 | for i in range( len(P) ): 53 | begin_value = P[i] 54 | end_value = Q[i] 55 | #print(count_semiprime_list[end_value]) 56 | #print(count_semiprime_list[begin_value]) 57 | # be very careful about the 'begin_value' (not included) 58 | answer_list[i] = count_semiprime_list[end_value] - count_semiprime_list[ (begin_value-1) ] 59 | #print(answer_list) 60 | 61 | return answer_list 62 | -------------------------------------------------------------------------------- /CountTriangles.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_list = A 8 | my_list.sort() 9 | # print(my_list) 10 | 11 | if len(A) < 3: 12 | return 0 13 | 14 | count_triangular = 0 15 | 16 | for index_one in range( len(A) -2): 17 | index_two = index_one + 1 18 | index_three = index_one + 2 19 | while (index_two< len(A)-1): 20 | if ( index_three my_list[index_three]) : 21 | index_three +=1 22 | else: 23 | count_triangular += (index_three - index_two - 1) 24 | index_two += 1 25 | 26 | return count_triangular 27 | -------------------------------------------------------------------------------- /CyclicRotation.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A, K): 5 | # write your code in Python 3.6 6 | 7 | # initialize array [0, 0, ..., 0] 8 | temp_array = [0] * len(A) 9 | 10 | for index in range( len(A) ) : 11 | new_index = (index + K) % len(A) # new position 12 | temp_array[new_index] = A[index] # set value 13 | 14 | return temp_array 15 | -------------------------------------------------------------------------------- /Distinct.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_dictionary = {} 8 | for item in A: 9 | if item not in my_dictionary: 10 | my_dictionary[item] = True 11 | 12 | # print(my_dictionary) 13 | 14 | num_distinct = len(my_dictionary) 15 | 16 | return num_distinct 17 | -------------------------------------------------------------------------------- /Dominator.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_dictionary = {} 8 | 9 | for item in A: 10 | my_dictionary[item] = 0 11 | 12 | for item in A: 13 | my_dictionary[item] += 1 14 | 15 | # print(my_dictionary) 16 | 17 | have_dominator = False 18 | value_dominator = -1 19 | index_dominator = -1 20 | 21 | for index in range( len(A) ): 22 | if my_dictionary[ A[index] ] > float( len(A) / 2): 23 | have_dominator = True 24 | value_dominator = A[index] 25 | index_dominator = index 26 | 27 | if have_dominator == False: 28 | return -1 29 | else: 30 | return index_dominator 31 | -------------------------------------------------------------------------------- /EquiLeader.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | # key point: 8 | # the equi-leader 'must' be leader in A 9 | 10 | # step 1: find the leader in A 11 | 12 | my_dictionary = {} 13 | 14 | for item in A: 15 | my_dictionary[item] = 0 16 | 17 | for item in A: 18 | my_dictionary[item] += 1 19 | 20 | have_leader = False 21 | leader_value = -1 22 | 23 | for item in A: 24 | if my_dictionary[item] > float( len(A) / 2): 25 | have_leader = True 26 | leader_value = item 27 | 28 | if have_leader == False: 29 | return 0 30 | 31 | # step 2: number of equi-leaders 32 | 33 | leader_count = [] 34 | num_leader = 0 35 | 36 | for item in A: 37 | if item == leader_value: 38 | num_leader += 1 39 | leader_count.append(num_leader) 40 | else: 41 | leader_count.append(num_leader) 42 | 43 | # print(leader_count) 44 | 45 | num_equi_leader = 0 46 | 47 | for index in range( len(A) ): 48 | total_num_leader = leader_count[len(A)-1] 49 | left_num_leader = leader_count[index] 50 | right_num_leader = total_num_leader - left_num_leader 51 | left_leader_threshold = float( (index+1) / 2) 52 | right_leader_threshold = float( (len(A)-index-1) / 2) 53 | if left_num_leader > left_leader_threshold and right_num_leader > right_leader_threshold: 54 | num_equi_leader += 1 55 | 56 | return num_equi_leader 57 | -------------------------------------------------------------------------------- /FibFrog_performance_50.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | #test_A = [1,1,1,1,1,1,1] 8 | #result = my_solution(test_A) 9 | #print(result) 10 | #print('end') 11 | 12 | result = my_solution(A) 13 | return result 14 | 15 | def my_solution(A): 16 | len_river = len(A) 17 | 18 | fabonacci = [] 19 | fabonacci.append(0) 20 | fabonacci.append(1) 21 | index = 1 22 | while fabonacci[index] <= len_river: 23 | index += 1 24 | temp = fabonacci[index-1] + fabonacci[index-2] 25 | fabonacci.append(temp) 26 | 27 | # print(fabonacci) 28 | # fabonacci = fabonacci[:-1] # remove the last element 29 | fabonacci = fabonacci[::-1] # reverse a list in Python 30 | # print(fabonacci) 31 | 32 | my_queue = [] 33 | my_dictionary = {-1:0} # position:-1, steps:0 34 | my_queue.append(my_dictionary) 35 | # print(my_queue) 36 | # print(len(my_queue)) 37 | 38 | index = 0 39 | while True: 40 | 41 | # cannot take element from queue anymore 42 | if len(my_queue)==index: 43 | # print( len(my_queue) ) 44 | # print( index ) 45 | return -1 46 | 47 | # get from queue 48 | temp_dictionary = my_queue[index] 49 | # print(temp_dictionary) 50 | 51 | # get key and value 52 | for key in temp_dictionary: 53 | current_position = key 54 | current_step = temp_dictionary[key] 55 | # print(current_position) 56 | # print(current_step) 57 | 58 | # from big to small 59 | for item in fabonacci: 60 | 61 | next_position = current_position + item 62 | 63 | # reach the other side 64 | if next_position == len_river: 65 | current_step += 1 66 | return current_step 67 | 68 | # can not jump 69 | elif next_position > len_river: 70 | pass 71 | elif next_position < 0 : 72 | pass 73 | elif A[next_position] ==0: 74 | pass 75 | 76 | # jump to next position 77 | else: 78 | current_step += 1 79 | temp_dictionary = {next_position: current_step} 80 | my_queue.append(temp_dictionary) 81 | 82 | A[next_position] = 0 # important 83 | 84 | index += 1 # take "next element" from queue 85 | -------------------------------------------------------------------------------- /Fish.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A, B): 5 | # write your code in Python 3.6 6 | 7 | downstream_stack = [] 8 | 9 | n = len(A) 10 | 11 | num_alive = n 12 | 13 | for i in range(n): 14 | 15 | # downstream 16 | if B[i] == 1: 17 | downstream_stack.append( A[i] ) 18 | 19 | # upstream 20 | if B[i] == 0: 21 | 22 | while len(downstream_stack) != 0: 23 | pop = downstream_stack.pop( len(downstream_stack)-1 ) 24 | if pop >= A[i]: 25 | num_alive -=1 26 | downstream_stack.append(pop) # just push back 27 | break 28 | elif pop < A[i]: 29 | num_alive -=1 30 | # keep looping (pop another downstrem fish) 31 | 32 | return num_alive 33 | -------------------------------------------------------------------------------- /FrogJmp.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(X, Y, D): 5 | # write your code in Python 3.6 6 | 7 | distance = Y - X 8 | 9 | num_jump_float = float(distance / D) 10 | num_jump_int = int(distance / D) 11 | 12 | num_jump_need = num_jump_int 13 | if num_jump_float > num_jump_int: 14 | num_jump_need += 1 15 | 16 | return num_jump_need 17 | 18 | -------------------------------------------------------------------------------- /FrogRiverOne.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(X, A): 5 | # write your code in Python 3.6 6 | 7 | my_set = set() 8 | 9 | for value in range(1, X+1): 10 | my_set.add( value ) 11 | 12 | for index in range(0, len(A) ): 13 | if A[index] in my_set: 14 | my_set.remove( A[index] ) 15 | if my_set == set(): 16 | return index 17 | 18 | return -1 19 | -------------------------------------------------------------------------------- /GenomicRangeQuery_v2.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(S, P, Q): 5 | # write your code in Python 3.6 6 | 7 | cumulative_A = [0] * ( len(S) +1 ) 8 | cumulative_C = [0] * ( len(S) +1 ) 9 | cumulative_G = [0] * ( len(S) +1 ) 10 | # cumulative_T = [0] * len(S) 11 | 12 | for i in range( len(S) ): 13 | 14 | if S[i] == 'A': 15 | cumulative_A[i+1] = cumulative_A[i] + 1 16 | cumulative_C[i+1] = cumulative_C[i] 17 | cumulative_G[i+1] = cumulative_G[i] 18 | elif S[i] == 'C': 19 | cumulative_A[i+1] = cumulative_A[i] 20 | cumulative_C[i+1] = cumulative_C[i] + 1 21 | cumulative_G[i+1] = cumulative_G[i] 22 | elif S[i] == 'G': 23 | cumulative_A[i+1] = cumulative_A[i] 24 | cumulative_C[i+1] = cumulative_C[i] 25 | cumulative_G[i+1] = cumulative_G[i] + 1 26 | else: 27 | cumulative_A[i+1] = cumulative_A[i] 28 | cumulative_C[i+1] = cumulative_C[i] 29 | cumulative_G[i+1] = cumulative_G[i] 30 | 31 | #print(cumulative_A) 32 | #print(cumulative_C) 33 | #print(cumulative_G) 34 | 35 | M = len(P) # =len(Q) 36 | 37 | result = [0] * M 38 | 39 | for j in range( M ): 40 | 41 | start_position = P[j] 42 | end_position = Q[j] + 1 # inclusive 43 | 44 | num_A = cumulative_A[end_position] - cumulative_A[start_position] 45 | num_C = cumulative_C[end_position] - cumulative_C[start_position] 46 | num_G = cumulative_G[end_position] - cumulative_G[start_position] 47 | 48 | if num_A > 0: 49 | result[j] = 1 50 | elif num_C > 0: 51 | result[j] = 2 52 | elif num_G > 0: 53 | result[j] = 3 54 | else: 55 | result[j] = 4 56 | 57 | return result 58 | 59 | 60 | -------------------------------------------------------------------------------- /Ladder_high_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A, B): 5 | # write your code in Python 3.6 6 | 7 | L = len(A) 8 | max_is_sufficient = max(A) 9 | 10 | # compute num of ways (by using Fibonacci) 11 | dictionary_num_ways = {} 12 | dictionary_num_ways[1] = 1 13 | dictionary_num_ways[2] = 2 # 1+1 or 2 14 | for x in range(3, max_is_sufficient+1, 1): 15 | dictionary_num_ways[x] = dictionary_num_ways[x-1] + dictionary_num_ways[x-2] 16 | # dictionary_num_ways[x-1] + '1' 17 | # or 18 | # dictionary_num_ways[x-2] + '2' 19 | dictionary_num_ways[x] = dictionary_num_ways[x] % (2 ** 30) 20 | # note: very important (to be quick in the extreme_large case) 21 | 22 | # compute results ( num_ways % (2 ** B[index]) ) 23 | result = [0] * L 24 | for index in range(L): 25 | num_ways = dictionary_num_ways[ A[index] ] 26 | each_result = num_ways % ( 2 ** B[index] ) 27 | result[index] = each_result 28 | 29 | return result 30 | -------------------------------------------------------------------------------- /Ladder_low_performance.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A, B): 5 | # write your code in Python 3.6 6 | 7 | L = len(A) 8 | 9 | # compute num of ways (by using Fibonacci) 10 | dictionary_num_ways = {} 11 | dictionary_num_ways[1] = 1 12 | dictionary_num_ways[2] = 2 # 1+1 or 2 13 | for x in range(3, L+1, 1): 14 | dictionary_num_ways[x] = dictionary_num_ways[x-1] + dictionary_num_ways[x-2] 15 | # dictionary_num_ways[x-1] + '1' 16 | # or 17 | # dictionary_num_ways[x-2] + '2' 18 | 19 | # compute results ( num_ways % (2 ** B[index]) ) 20 | result = [0] * L 21 | for index in range(L): 22 | num_ways = dictionary_num_ways[ A[index] ] 23 | each_result = num_ways % ( 2 ** B[index] ) 24 | result[index] = each_result 25 | 26 | return result 27 | -------------------------------------------------------------------------------- /MaxCounters_v3.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(N, A): 5 | # write your code in Python 3.6 6 | 7 | num_counters = N 8 | num_operations = len(A) 9 | 10 | my_list = [0] * N 11 | 12 | max_value = 0 13 | min_value = 0 14 | 15 | for operation in A: 16 | 17 | if operation < N+1: 18 | 19 | if my_list[operation-1] < min_value: 20 | my_list[operation-1] = min_value 21 | 22 | if my_list[operation-1] == max_value: 23 | max_value += 1 24 | 25 | my_list[operation-1] += 1 26 | 27 | 28 | elif operation == N+1: 29 | #max_value = max(my_list) 30 | #my_list = [max_value] * N 31 | min_value = max_value 32 | 33 | # print(my_list) 34 | 35 | for i in range(num_counters): 36 | if my_list[i] < min_value: 37 | my_list[i] = min_value 38 | 39 | return my_list 40 | -------------------------------------------------------------------------------- /MaxDoubleSliceSum.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | #special case: 8 | if (len(A) <= 3): 9 | return 0 10 | 11 | # main idea: 12 | # use "golden slice algorithm" O(n) 13 | # take maxEnding[i] = max( 0, maxEnding[i-1] + A[i] ) <--- important~!! 14 | # explanation : 15 | # At the end of each slice, we decide whether its value 16 | # is going to be carried to the next element's computation 17 | # based on whether the value is "negative or positive". <--- "key point" 18 | # If positive, we carry it (so it contributes to the next slice) 19 | # Otherwise we start from "0" 20 | 21 | #(X, Y, Z) 22 | # 1st slice: A[X+1] + ... + A[Y-1] 23 | # 2nd slice: A[Y+1] + ... + A[Z-1] 24 | # Key Point: 25 | # The array will be split at "Y" 26 | # main idea: 27 | # if the middle point is "Y", 28 | # find "maxLeft" and "maxRight" 29 | 30 | maxLeft = [0] * len(A) 31 | maxRight = [0] * len(A) 32 | 33 | # 1) find "maxLeft" 34 | # maxLeft[i] is the maximum sum "contiguous subsequence" ending at index i 35 | # note: because it is "contiguous", we only need the ending index (important) 36 | 37 | for index in range( 1, len(A) ): 38 | # be careful: from i=1 (because of maxLeft[i-1]) 39 | maxLeft[index] = max(0, maxLeft[index-1]+A[index] ); 40 | # golden slice algorithm: max(0, maxLeft[i-1]+A[i] ) 41 | 42 | # 2) find "maxRight" 43 | for index in range( len(A)-2, -1, -1 ): 44 | # be careful: from i=A.length-2 (because of maxLeft[i+1]) 45 | maxRight[index] = max(0, maxRight[index+1]+A[index] ); 46 | # golden slice algorithm: Math.max(0, maxRight[i+1]+A[i] ) 47 | 48 | # 3) find the maximum of "maxLeft + maxRight" 49 | maxDoubleSlice = A[1] + A[len(A)-2]; 50 | for index in range( 1, len(A)-1 ): 51 | if (maxLeft[index-1] + maxRight[index+1]) > maxDoubleSlice: 52 | maxDoubleSlice = maxLeft[index-1] + maxRight[index+1] 53 | #print(maxLeft[index-1]) 54 | #print(maxRight[index+1]) 55 | # be careful: "not" maxLeft[i] + maxRight[i] 56 | # left end at "i-1", and right begins at "i+1" 57 | 58 | return maxDoubleSlice; 59 | -------------------------------------------------------------------------------- /MaxNonoverlappingSegments.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A, B): 5 | # write your code in Python 3.6 6 | 7 | # the array is already sorted by B (the right end) 8 | 9 | # special cases (zero or only one) 10 | if len(A) == 0: 11 | return 0 12 | elif len(A) == 1: 13 | return 1 14 | 15 | # keep 'the current right-end' and 'the current left-end' 16 | current_right_end = B[0] 17 | current_left_end = A[0] 18 | 19 | num_non_overlapping = 1 20 | 21 | for index in range( len(A) ): 22 | current_left_end = A[index] # moving the left-end 23 | if current_left_end > current_right_end: 24 | num_non_overlapping += 1 25 | current_right_end = B[index] # update the right-end 26 | 27 | return num_non_overlapping 28 | -------------------------------------------------------------------------------- /MaxProductOfThree.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_sorted = sorted(A) 8 | 9 | possible_max_1 = my_sorted[len(A)-1] * my_sorted[len(A)-2] * my_sorted[len(A)-3] 10 | possible_max_2 = my_sorted[0] * my_sorted[1] * my_sorted[len(A)-1] 11 | 12 | max_product = max(possible_max_1, possible_max_2) 13 | 14 | return max_product 15 | -------------------------------------------------------------------------------- /MaxProfit.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | # key point: try to use only one for-loop O(n) 8 | 9 | if len(A)==0: 10 | return 0 11 | 12 | max_profit = 0 13 | min_price = A[0] 14 | 15 | for item in A: 16 | max_profit = max( max_profit, item - min_price ) 17 | min_price = min( min_price, item ) 18 | 19 | return max_profit 20 | -------------------------------------------------------------------------------- /MaxSliceSum.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import sys 5 | 6 | def solution(A): 7 | # write your code in Python 3.6 8 | 9 | # max_value = sys.maxsize 10 | # min_value = -sys.maxsize -1 11 | 12 | local_max_sum = A[0] 13 | global_max_sum = A[0] 14 | 15 | for index in range( 1, len(A) ): 16 | # note: be careful about the negative value (we may recount from A[index]) 17 | local_max_sum = max( local_max_sum + A[index], A[index] ) 18 | global_max_sum = max( global_max_sum, local_max_sum ) 19 | 20 | # special case: all negtive value(s) 21 | if max(A) < 0: 22 | global_max_sum = max(A) 23 | 24 | return global_max_sum 25 | -------------------------------------------------------------------------------- /MinAvgTwoSlice_v2.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | # The trick to this problem is 8 | # that the min average slice has "the length of 2 or 3" 9 | # So, we only need to calculate the avg of the slices of length 2 and 3 10 | 11 | min_average = float('inf') 12 | # note: float('-inf') 13 | min_position = 0 14 | 15 | for i in range( len(A)-2 ): 16 | 17 | average_len_2 = float( (A[i] + A[i+1]) / 2) 18 | average_len_3 = float( (A[i] + A[i+1] + A[i+2]) / 3) 19 | 20 | #print(i) 21 | #print(average_len_2) 22 | #print(average_len_3) 23 | 24 | current_min = min(average_len_2, average_len_3) 25 | if current_min < min_average: 26 | min_average = current_min 27 | min_position = i 28 | 29 | #note: for the last missing case 30 | #case: avg of length of 2 "A[ len(A)-2 ] + A[ len(A)-1 ]" 31 | last_average = float( (A[len(A)-2] + A[len(A)-1]) / 2 ) 32 | if last_average < min_average: 33 | min_average = last_average 34 | min_position = len(A)-2 35 | 36 | return min_position 37 | -------------------------------------------------------------------------------- /MinMaxDivision.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import math 5 | 6 | def solution(K, M, A): 7 | # write your code in Python 3.6 8 | 9 | # try to find minMaxSum 10 | # we can try 'minMaxSum' by using binary search (fast) 11 | # but, we need to know 12 | # min_possible_sum and max_possible_sum 13 | # so, we can try mid_possible_sum (using binary search) 14 | 15 | # 1. find min_possible_sum and max_possible_sum 16 | min_possible_sum = 0 17 | max_possible_sum = 0 18 | for item in A: 19 | min_possible_sum = max(min_possible_sum, item) # at least one element (large) 20 | max_possible_sum += item # at most all elements (sum of all) 21 | #print(min_possible_sum) 22 | #print(max_possible_sum) 23 | 24 | # 3. check if mid_possible_sum is 'ok' or not (define the method) 25 | def check_if_mid_sum_possible(K, M, A, mid_sum): 26 | num_block_allowed = K 27 | current_block_sum = 0 28 | for item in A: 29 | current_block_sum += item 30 | if current_block_sum > mid_sum: # need another block 31 | num_block_allowed -= 1 32 | current_block_sum = item # important (the item in next block) 33 | if num_block_allowed ==0: 34 | return False 35 | # all blocks can be smaller than (or equal to) mid_sum 36 | return True 37 | 38 | # 2. binary search 39 | result = max_possible_sum 40 | 41 | while min_possible_sum <= max_possible_sum: 42 | mid_possible_sum = math.ceil( (min_possible_sum + max_possible_sum) / 2 ) 43 | # print(mid_possible_sum) 44 | 45 | # try smaller 46 | is_sum_possible = check_if_mid_sum_possible(K, M, A, mid_possible_sum) 47 | # print(is_sum_possible) 48 | if is_sum_possible == True: 49 | result = mid_possible_sum 50 | max_possible_sum = mid_possible_sum - 1 51 | 52 | # try bigger 53 | else: 54 | min_possible_sum = mid_possible_sum + 1 55 | 56 | return result 57 | -------------------------------------------------------------------------------- /MinPerimeterRectangle.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import math 5 | 6 | def solution(N): 7 | # write your code in Python 3.6 8 | 9 | my_perimeter_list = [] 10 | 11 | for n in range( 1, int(math.sqrt(N))+1 ): 12 | if( N % n ==0): 13 | one_side = n 14 | another_side = int( N/n ) 15 | perimeter = 2 * (one_side + another_side) 16 | my_perimeter_list.append(perimeter) 17 | 18 | min_perimeter = min( my_perimeter_list ) 19 | 20 | return min_perimeter 21 | -------------------------------------------------------------------------------- /MissingInteger.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_dictionary = {} 8 | 9 | for item in A: 10 | if item not in my_dictionary: 11 | my_dictionary[item] = 1 12 | 13 | # print(my_dictionary) 14 | 15 | for i in range(1, len(A)+1 ): 16 | if i not in my_dictionary: 17 | return i 18 | 19 | return len(A)+1 20 | -------------------------------------------------------------------------------- /Nesting.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(S): 5 | # write your code in Python 3.6 6 | 7 | my_stack = [] 8 | 9 | for char in S: 10 | 11 | if char == '(': 12 | my_stack.append(char) 13 | 14 | elif char == ')': 15 | 16 | # note: check if the stack is empty or not (be careful) 17 | if len(my_stack) == 0: 18 | return 0 19 | 20 | else: 21 | pop = my_stack.pop( len(my_stack)-1 ) 22 | 23 | # note: check if the stack is empty or not (be careful) 24 | if len(my_stack) != 0: 25 | return 0 26 | else: 27 | return 1 28 | -------------------------------------------------------------------------------- /NumberOfDiscIntersections_v2.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | lower = [0] * len(A) 8 | upper = [0] * len(A) 9 | 10 | # all the lower and upper points 11 | for i in range( len(A) ): 12 | lower[i] = i - A[i] 13 | upper[i] = i + A[i] 14 | 15 | sorted_lower = sorted(lower) 16 | sorted_upper = sorted(upper) 17 | 18 | intersection = 0 19 | j = 0 # for lower points 20 | 21 | # scan the upper points 22 | for i in range( len(A) ): 23 | # scan the lower points with condition (note: sorted) 24 | while j < len(A) and sorted_upper[i] >= sorted_lower[j]: 25 | intersection += j 26 | intersection -= i 27 | j += 1 28 | 29 | if intersection > 10_000_000: 30 | return -1 31 | 32 | return intersection 33 | -------------------------------------------------------------------------------- /NumberSolitaire.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import sys 5 | 6 | def solution(A): 7 | # write your code in Python 3.6 8 | 9 | len_a = len(A) 10 | 11 | # initialize 'dp' as [ A[0], 0, 0, ..., 0] 12 | dp = [A[0]] + ([0]*(len_a-1)) 13 | 14 | # print(dp) 15 | 16 | # build up 'dp' (bottom-up) 17 | for index in range(1, len_a): 18 | 19 | # note: the min_int is '(-sys.maxsize)-1' 20 | temp_max = (-sys.maxsize)-1 21 | 22 | # there might be '6' possible combination (at most) 23 | for die_number in range(1, 6+1): 24 | if index-die_number >= 0: 25 | temp_max = max( dp[index-die_number] + A[index], temp_max ) # super important! 26 | 27 | # keep the max (temp_max) 28 | dp[index] = temp_max 29 | 30 | # print(dp) 31 | 32 | # return the last one 33 | return dp[len_a-1] 34 | -------------------------------------------------------------------------------- /OddOccurrencesInArray.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | my_dictionary = {} 8 | odd_element = -1 9 | 10 | # create dictionary 11 | for item in A: 12 | my_dictionary[item] = 0 13 | 14 | # counting 15 | for item in A: 16 | my_dictionary[item] += 1 17 | 18 | # find odd element 19 | for key in my_dictionary: 20 | if (my_dictionary[key] % 2 != 0): 21 | odd_element = key 22 | 23 | return odd_element 24 | -------------------------------------------------------------------------------- /PassingCars.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | cumulative_sum = [0] * len(A) 8 | 9 | cumulative_sum[0] = A[0] 10 | 11 | for i in range( 1, len(A) ): 12 | cumulative_sum[i] = cumulative_sum[i-1] + A[i] 13 | 14 | # print(cumulative_sum) 15 | 16 | total_passing = 0 17 | 18 | for i in range( len(A) ): 19 | if A[i] == 0: 20 | passing = cumulative_sum[len(A)-1] - cumulative_sum[i] 21 | total_passing += passing 22 | if total_passing > 1000000000: 23 | return -1 24 | 25 | return total_passing 26 | -------------------------------------------------------------------------------- /Peaks.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | peak_list = [] 8 | 9 | # find peaks 10 | for index in range( 1, len(A)-1 ): 11 | if A[index] > A[index-1] and A[index] > A[index+1]: 12 | peak_list.append( index ) 13 | 14 | # print(peak_list) 15 | 16 | for num_block in range( len(A), 0 , -1 ): 17 | 18 | # check 'blocks containing the same number of elements' 19 | if ( len(A) % num_block == 0): 20 | 21 | block_size = int( len(A)/num_block ) 22 | ith_block = 0 23 | num_block_has_peak = 0 24 | 25 | for peak_index in peak_list: 26 | 27 | # check if any peak is within the ith block 28 | if int( peak_index/block_size) == ith_block: 29 | num_block_has_peak += 1 30 | ith_block += 1 31 | 32 | # chek if all blocks have at least one peak 33 | if num_block_has_peak == num_block: 34 | return num_block 35 | 36 | return 0 37 | -------------------------------------------------------------------------------- /PermCheck.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | sorted_array = sorted(A) 8 | 9 | expected_num = 1 10 | for item in sorted_array: 11 | if item == expected_num: 12 | expected_num += 1 13 | else: 14 | return 0 15 | 16 | return 1 17 | -------------------------------------------------------------------------------- /PermMissingElem.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | N = len(A) 8 | 9 | my_list = [0] * (N+2) 10 | 11 | missing_element = -1 12 | 13 | for index in range(0, N): 14 | my_list[ A[index] ] += 1 15 | 16 | for index in range(1, N+2): 17 | if my_list[ index ] == 0: 18 | missing_element = index 19 | 20 | return missing_element 21 | -------------------------------------------------------------------------------- /Python_Notes_Part1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/Python_Notes_Part1.docx -------------------------------------------------------------------------------- /Python_Notes_part2.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/Python_Notes_part2.docx -------------------------------------------------------------------------------- /Python_Useful_Functions.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mickey0521/Codility-Python/0423da9ab191b5b65035190782f238520f2b8330/Python_Useful_Functions.docx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codility-Python 2 | 3 | My Solutions to [Codility Lessons](https://app.codility.com/programmers/lessons/1-iterations/) 4 | are listed as follows (100% performance with comments) (using Python): 5 | 6 | Lesson 1 Iterations [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/1-Iterations_cckao.pdf) 7 | - [BinaryGap](https://github.com/Mickey0521/Codility-Python/blob/master/BinaryGap.py) 8 | 9 | Lesson 2 Arrays [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/2-Arrays_cckao.pdf) 10 | - [OddOccurrencesInArray](https://github.com/Mickey0521/Codility-Python/blob/master/OddOccurrencesInArray.py) 11 | - [CyclicRotation](https://github.com/Mickey0521/Codility-Python/blob/master/CyclicRotation.py) 12 | 13 | Lesson 3 Time Complexity [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/3-TimeComplexity_cckao.pdf) 14 | - [FrogJmp](https://github.com/Mickey0521/Codility-Python/blob/master/FrogJmp.py) 15 | - [PermMissingElem](https://github.com/Mickey0521/Codility-Python/blob/master/PermMissingElem.py) 16 | - [TapeEquilibrium](https://github.com/Mickey0521/Codility-Python/blob/master/TapeEquilibrium.py) 17 | 18 | Lesson 4 Counting Elements [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/4-CountingElements_cckao.pdf) 19 | - [PermCheck](https://github.com/Mickey0521/Codility-Python/blob/master/PermCheck.py) 20 | - [FrogRiverOne](https://github.com/Mickey0521/Codility-Python/blob/master/FrogRiverOne.py) 21 | - [MissingInteger](https://github.com/Mickey0521/Codility-Python/blob/master/MissingInteger.py) 22 | - [MaxCounters](https://github.com/Mickey0521/Codility-Python/blob/master/MaxCounters_v3.py) 23 | 24 | Lesson 5 Prefix Sums [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/5-PrefixSums_cckao.pdf) 25 | - [CountDiv](https://github.com/Mickey0521/Codility-Python/blob/master/CountDiv_v2.py) 26 | - [PassingCars](https://github.com/Mickey0521/Codility-Python/blob/master/PassingCars.py) 27 | - [GenomicRangeQuery](https://github.com/Mickey0521/Codility-Python/blob/master/GenomicRangeQuery_v2.py) (respectable) 28 | - [MinAvgTwoSlice](https://github.com/Mickey0521/Codility-Python/blob/master/MinAvgTwoSlice_v2.py) (respectable) 29 | 30 | Lesson 6 Sorting [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/6-Sorting_cckao.pdf) 31 | - [Distinct](https://github.com/Mickey0521/Codility-Python/blob/master/Distinct.py) 32 | - [Triangle](https://github.com/Mickey0521/Codility-Python/blob/master/Triangle.py) 33 | - [MaxProductOfThree](https://github.com/Mickey0521/Codility-Python/blob/master/MaxProductOfThree.py) 34 | - [NumberOfDiscIntersections](https://github.com/Mickey0521/Codility-Python/blob/master/NumberOfDiscIntersections_v2.py) (respectable) 35 | 36 | Lesson 7 Stacks and Queues [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/7-Stacks_cckao.pdf) 37 | - [StoneWall](https://github.com/Mickey0521/Codility-Python/blob/master/StoneWall.py) 38 | - [Brackets](https://github.com/Mickey0521/Codility-Python/blob/master/Brackets_v2.py) 39 | - [Nesting](https://github.com/Mickey0521/Codility-Python/blob/master/Nesting.py) 40 | - [Fish](https://github.com/Mickey0521/Codility-Python/blob/master/Fish.py) 41 | 42 | Lesson 8 Leader [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/8-Leader_cckao.pdf) 43 | - [EquiLeader](https://github.com/Mickey0521/Codility-Python/blob/master/EquiLeader.py) 44 | - [Dominator](https://github.com/Mickey0521/Codility-Python/blob/master/Dominator.py) 45 | 46 | Lesson 9 Maximum slice problem [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/9-MaxSlice_cckao.pdf) 47 | - [MaxDoubleSliceSum](https://github.com/Mickey0521/Codility-Python/blob/master/MaxDoubleSliceSum.py) 48 | - [MaxProfit](https://github.com/Mickey0521/Codility-Python/blob/master/MaxProfit.py) 49 | - [MaxSliceSum](https://github.com/Mickey0521/Codility-Python/blob/master/MaxSliceSum.py) 50 | 51 | Lesson 10 Prime and composite numbers [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/10-PrimeNumbers_cckao.pdf) 52 | - [MinPerimeterRectangle](https://github.com/Mickey0521/Codility-Python/blob/master/MinPerimeterRectangle.py) 53 | - [CountFactors](https://github.com/Mickey0521/Codility-Python/blob/master/CountFactors.py) 54 | - [Peaks](https://github.com/Mickey0521/Codility-Python/blob/master/Peaks.py) (respectable) 55 | - Flags (respectable) 56 | 57 | Lesson 11 Sieve of Eratosthenes [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/11-Sieve_cckao.pdf) 58 | - [CountSemiprimes](https://github.com/Mickey0521/Codility-Python/blob/master/CountSemiprimes_high_performance.py) 59 | - [CountNonDivisible (respectable)](https://github.com/Mickey0521/Codility-Python/blob/master/CountNonDivisible.py) 60 | 61 | Lesson 12 Euclidean algorithm [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/12-Gcd_cckao.pdf) 62 | - [ChocolatesByNumbers](https://github.com/Mickey0521/Codility-Python/blob/master/ChocolatesByNumbers_high_performance.py) 63 | - CommonPrimeDivisors (respectable) 64 | 65 | Lesson 13 Fibonacci numbers [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/13-Fibonacci_cckao.pdf) 66 | - [Ladder (respectable)](https://github.com/Mickey0521/Codility-Python/blob/master/Ladder_high_performance.py) 67 | - [FibFrog (respectable)](https://github.com/Mickey0521/Codility-Python/blob/master/FibFrog_performance_50.py) 68 | 69 | Lesson 14 Binary search algorithm [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/14-BinarySearch_cckao.pdf) 70 | - [MinMaxDivision (respectable)](https://github.com/Mickey0521/Codility-Python/blob/master/MinMaxDivision.py) 71 | - NailingPlanks (respectable) 72 | 73 | Lesson 15 Caterpillar method [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/15-CaterpillarMethod_cckao.pdf) 74 | - [AbsDistinct](https://github.com/Mickey0521/Codility-Python/blob/master/AbsDistinct.py) 75 | - [CountDistinctSlices](https://github.com/Mickey0521/Codility-Python/blob/master/CountDistinctSlices_high_performance.py) 76 | - [CountTriangles](https://github.com/Mickey0521/Codility-Python/blob/master/CountTriangles.py) 77 | - MinAbsSumOfTwo (respectable) 78 | 79 | Lesson 16 Greedy algorithms [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/16-GreedyAlgorithms_cckao.pdf) 80 | - [MaxNonoverlappingSegments](https://github.com/Mickey0521/Codility-Python/blob/master/MaxNonoverlappingSegments.py) 81 | - [TieRopes](https://github.com/Mickey0521/Codility-Python/blob/master/TieRopes.py) 82 | 83 | Lesson 17 Dynamic programming [PDF](https://github.com/Mickey0521/Codility-Python/blob/master/17-DynamicProgramming_cckao.pdf) 84 | - [NumberSolitaire (respectable)](https://github.com/Mickey0521/Codility-Python/blob/master/NumberSolitaire.py) 85 | - MinAbsSum (ambitious) 86 | 87 | My Python Notes (when i tried to solve the Codility problems) (feel free to download the .docx files) 88 | - [Python Notes Part1](https://github.com/Mickey0521/Codility-Python/blob/master/Python_Notes_Part1.docx) 89 | - [Python Notes Part2](https://github.com/Mickey0521/Codility-Python/blob/master/Python_Notes_part2.docx) 90 | - [Python Useful Functions](https://github.com/Mickey0521/Codility-Python/blob/master/Python_Useful_Functions.docx) 91 | -------------------------------------------------------------------------------- /StoneWall.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(H): 5 | # write your code in Python 3.6 6 | 7 | # key point: two different cases 8 | # case 1: from high to low 9 | # case 2: from low to high 10 | 11 | # use stack 12 | my_stack = [] 13 | 14 | num_block = 0 15 | 16 | for i in range( len(H) ): 17 | 18 | # "stack is not empty" and "from high to low"; 19 | # then, "pop" (it is the key point) 20 | while ( len(my_stack) !=0 ) and ( H[i] < my_stack[len(my_stack)-1] ): 21 | my_stack.pop( len(my_stack)-1 ) 22 | 23 | # empty (stack) 24 | if len(my_stack) == 0: 25 | num_block += 1 26 | my_stack.append(H[i]) 27 | 28 | # the height is the same 29 | elif H[i] == my_stack[ len(my_stack)-1 ] : 30 | # do nothing (no need to add any block) 31 | pass 32 | 33 | # from low to high 34 | elif H[i] >= my_stack[ len(my_stack)-1 ] : 35 | num_block += 1 36 | my_stack.append(H[i]) 37 | 38 | return num_block 39 | -------------------------------------------------------------------------------- /TapeEquilibrium.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import sys 5 | 6 | def solution(A): 7 | # write your code in Python 3.6 8 | 9 | INT_MAX = sys.maxsize 10 | INT_MIN = -sys.maxsize-1 11 | 12 | min_diff = INT_MAX 13 | 14 | for index in range(0, len(A) ): 15 | 16 | first_total = 0 17 | for j in range(0, index): 18 | first_total += A[j] 19 | 20 | second_total = 0 21 | for k in range(index, len(A) ): 22 | second_total += A[k] 23 | 24 | current_difference = abs( first_total - second_total ) 25 | 26 | min_diff = min( min_diff, current_difference ) 27 | 28 | return min_diff 29 | -------------------------------------------------------------------------------- /TieRopes.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | import copy 5 | 6 | def solution(K, A): 7 | # write your code in Python 3.6 8 | 9 | all_rope_list = [] 10 | 11 | current_length = 0 12 | current_rope_list = [] 13 | for item in A: 14 | current_length += item 15 | current_rope_list.append(item) 16 | # print(current_length) 17 | if current_length >= K: 18 | current_length = 0 19 | # all_rope_list.append( current_rope_list ) 20 | # very important: need to use 'copy.copy()' for the current list 21 | # print(current_rope_list) 22 | all_rope_list.append( copy.copy(current_rope_list) ) 23 | # print(all_rope_list) 24 | current_rope_list.clear() 25 | 26 | num_rope = len(all_rope_list) 27 | return num_rope 28 | -------------------------------------------------------------------------------- /Triangle.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | # key point: sort the array 8 | sorted_array = sorted(A) 9 | 10 | for index in range( len(A)-2 ): 11 | if sorted_array[index] + sorted_array[index+1] > sorted_array[index+2]: 12 | return 1 13 | 14 | return 0 15 | --------------------------------------------------------------------------------