-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_015.py
More file actions
35 lines (24 loc) · 768 Bytes
/
Problem_015.py
File metadata and controls
35 lines (24 loc) · 768 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
35
import time
tri_numbers = [0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231]
store = dict()
def add_to_store(num, level, count):
if num not in store:
store[num] = dict()
store[num][level] = count
def get_store_count(num, level):
sub_store = store.get(num, 0)
if sub_store == 0:
return 0
return sub_store.get(level, 0)
def triangle_recur(num, level):
if level == 0:
return tri_numbers[num]
count = get_store_count(num, level)
if count != 0:
return count
for i in range(1, num + 1):
count += triangle_recur(i, level - 1)
add_to_store(num, level, count)
return count
grid_size = 20
print(triangle_recur(grid_size + 1, grid_size - 2))