File tree Expand file tree Collapse file tree 1 file changed +23
-8
lines changed
src/hackerrank/interview_preparation_kit/recursion_and_backtracking Expand file tree Collapse file tree 1 file changed +23
-8
lines changed Original file line number Diff line number Diff line change 4
4
5
5
from typing import Dict
6
6
7
+ TOP_LIMIT = 10 ** 10 + 7
8
+ STEPS_LIMIT = 3
7
9
8
- def step_perms_comput_with_cache (n_steps : int , cache : Dict [int , int ]) -> int :
10
+
11
+ def step_perms_comput_with_cache (
12
+ n_steps : int ,
13
+ cache : Dict [int , int ],
14
+ steps_limit : int ) -> int :
15
+
16
+ # Base cases
9
17
if 0 <= n_steps <= 2 :
10
18
return n_steps
11
- if n_steps == 3 :
12
- return 4
13
19
14
20
result = 0
15
- for i in range (1 , 4 ):
16
- if n_steps - i not in cache :
17
- cache [n_steps - i ] = step_perms_comput_with_cache (n_steps - i , cache )
21
+ for i in range (1 , min (steps_limit , n_steps ) + 1 ):
22
+ search_key : int = n_steps - i
23
+
24
+ if search_key not in cache :
25
+ cache [search_key ] = step_perms_comput_with_cache (
26
+ search_key ,
27
+ cache ,
28
+ steps_limit
29
+ )
18
30
19
31
result += cache [n_steps - i ]
20
32
21
- return result
33
+ # if n_steps <= steps_limit:
34
+ # result += 1
35
+
36
+ return (result + 1 ) if n_steps <= steps_limit else result
22
37
23
38
24
39
def step_perms (n_steps : int ) -> int :
25
40
initial_cache : Dict [int , int ] = {}
26
- return step_perms_comput_with_cache (n_steps , initial_cache ) % ( 10 ** 10 + 7 )
41
+ return step_perms_comput_with_cache (n_steps , initial_cache , STEPS_LIMIT ) % TOP_LIMIT
You can’t perform that action at this time.
0 commit comments