-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem191.py
More file actions
28 lines (28 loc) · 807 Bytes
/
problem191.py
File metadata and controls
28 lines (28 loc) · 807 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
import time
start = time.time()
# Recursive dynamic program
def recur(days,late,absent):
## forfeit prize
if absent > 2 or late > 1:
return 0
## no days left and still alive then count as win
elif days == 0:
return 1
## if not finished yet
elif (days,late,absent) in D:
## Try again
return D[(days,late,absent)]
## one more day on time
ot = recur(days - 1,late,0)
## one more day absent
ab = recur(days - 1,late,absent + 1)
# one more day late
la = recur(days - 1,late + 1,0)
# Add to dictionary
D[(days,late,absent)] = ot + ab + la
## return the sum of the number of days on time, late and absent
return ot + ab + la
# create empty dictionary
D = {}
print(recur(30,0,0))
print(str(time.time() - start) + "ms")