forked from Mickey0521/Codility-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLadder_high_performance.py
More file actions
29 lines (24 loc) · 986 Bytes
/
Ladder_high_performance.py
File metadata and controls
29 lines (24 loc) · 986 Bytes
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
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A, B):
# write your code in Python 3.6
L = len(A)
max_is_sufficient = max(A)
# compute num of ways (by using Fibonacci)
dictionary_num_ways = {}
dictionary_num_ways[1] = 1
dictionary_num_ways[2] = 2 # 1+1 or 2
for x in range(3, max_is_sufficient+1, 1):
dictionary_num_ways[x] = dictionary_num_ways[x-1] + dictionary_num_ways[x-2]
# dictionary_num_ways[x-1] + '1'
# or
# dictionary_num_ways[x-2] + '2'
dictionary_num_ways[x] = dictionary_num_ways[x] % (2 ** 30)
# note: very important (to be quick in the extreme_large case)
# compute results ( num_ways % (2 ** B[index]) )
result = [0] * L
for index in range(L):
num_ways = dictionary_num_ways[ A[index] ]
each_result = num_ways % ( 2 ** B[index] )
result[index] = each_result
return result