模板
dfs
回溯法
三个核心要素:路径、选择列表、结束条件
result = []
路径 = []
def backtrack(ind):
if ind 最后一个:
res.append(路径)
return
for 选择 in 选择列表:
做选择
backtrack(ind+1)
撤销选择
单调栈
遇到比栈顶大的元素,直接append,
遇到比栈顶小的元素,找到合适的位置,替换。
单源最短路径Digkstra
节点分成两类:(1)已经确定距离的节点 (2)没有确定的节点
每次从未确定的节点中选择一个与起点距离最点的点,然后更新从起点到其他所有未确定节点的距离。
二分法
二分法查找相同值取最右边的插入位置
def bisect_right(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi =