Skip to content

Commit 86843ed

Browse files
committed
upload Lesson 17
1 parent 203660f commit 86843ed

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

17-DynamicProgramming_cckao.pdf

404 KB
Binary file not shown.

NumberSolitaire.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+

0 commit comments

Comments
 (0)