-
Notifications
You must be signed in to change notification settings - Fork 0
/
#Templates#.py
48 lines (43 loc) · 931 Bytes
/
#Templates#.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
#DP
dp = ...
# create dp array
# add padding if needed
dp[0][0] = ...
# init dp arrray
# base case
for i ...
for j ...
...
dp[i][j] = ...
# transition
return dp[n][m]
# Recursion with memroization
mem = ...
# create mem dict or use __init__ to create
def dp(i, j, ...)
if base_case(i, j): return ...
# base_case
if (i, j) not int mem:
mem[(i,j)] = ...
# transition
return mem[(i, j)]
return dp(n, m)
# Backtrack
def backtrack(i,n, paramas...):
if i == n:
return ans
else:
backtrack()...
result = []
backtrack(i, n, params...)
return result
#Divide and conquer
def divide(array, ...):
if least_condition: return sth
mid = len(lists) // 2
l = self.divide(lists[:mid])
r = self.divide(lists[mid:])
return self.subproblem()
def subproblem():
...
# slow runner and fast runner