Open wanghsinche opened 4 years ago
To use dynamic programing to solve a problem, there are some steps we can follow with:
For example, an iterative method to solve dynamic programing problem:
cache = dict()
def iter_dp(target):
## initial condition
cache[1] = 1
cache[2] = 2
idx = 3
while idx <= target:
cache[idx] = f(cache)
return cache[target]
sliding window template
save the range if the range is satisfied with all of the conditions.
def sliding(array, condition):
state = None
left, right = 0, 0
while right < len(array):
updateState(state, array[right])
# shrink left pointer until the subarray from left to right can not match the condition anymore
while state with left pointer meets the condition:
save ans
left += 1
# right pointer grows
right += 1
return all ans
There are a nice resource. https://protegejj.gitbooks.io/my-algorithm-summary/data-structure.html
https://www.techinterviewhandbook.org/ a great coding interview handbook