forked from Mickey0521/Codility-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberSolitaire.py
More file actions
34 lines (22 loc) · 802 Bytes
/
NumberSolitaire.py
File metadata and controls
34 lines (22 loc) · 802 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
30
31
32
33
34
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import sys
def solution(A):
# write your code in Python 3.6
len_a = len(A)
# initialize 'dp' as [ A[0], 0, 0, ..., 0]
dp = [A[0]] + ([0]*(len_a-1))
# print(dp)
# build up 'dp' (bottom-up)
for index in range(1, len_a):
# note: the min_int is '(-sys.maxsize)-1'
temp_max = (-sys.maxsize)-1
# there might be '6' possible combination (at most)
for die_number in range(1, 6+1):
if index-die_number >= 0:
temp_max = max( dp[index-die_number] + A[index], temp_max ) # super important!
# keep the max (temp_max)
dp[index] = temp_max
# print(dp)
# return the last one
return dp[len_a-1]