Dynamic Programming
Posted sarah-zhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamic Programming相关的知识,希望对你有一定的参考价值。
0.
1. examples:
def fib(n):
if n == 1 or n == 2:
result = 1
else
result = fib(n-1) + fib(n-2)
return result
this is very in-efficient, O(2^n), and we use the following solution, which is called memoized solution
def fib(n, memo):
if memo[n] != null:
return memo[n]
if n ==1 or n ==2:
result = 1
else
result = fib(n-1, memo) + fib(n-2, memo)
memo[n] = result
return result
Memo is an array, store memo = {1, 1, 2, 3, 5, 8 ,...}
def fib_bottom_up(n):
if n ==1 or n ==2:
return 1
bottom_up = new int[n+1]
bottom_up[1] =1
bottom_up[2] =1
for i from 3 to n:
bottom_up[i] = bottom_up[i-1] + bottom[i-2]
return bottom_up[n]
以上是关于Dynamic Programming的主要内容,如果未能解决你的问题,请参考以下文章
_CastError(类型 '_InternalLinkedHashMap<dynamic, dynamic>' 不是类型转换中类型 'Map<String, dynamic>
_InternalLinkedHashMap<String, dynamic>' 不是类型 'List<Map<dynamic, dynamic>>' 的子类型
类型“_InternalLinkedHashMap<dynamic, dynamic>”不是“函数结果”的“List<Map<String, dynamic>>”类
Flutter json解码_TypeError(类型'List <dynamic>'不是'Map <dynamic,dynamic>'类型的子类型)[关闭]
参数类型 'List<Series<dynamic, dynamic>>' 不能分配给参数类型 'List<Series<dynamic, String*>*
参数类型 'List<Series<dynamic, dynamic>>' 不能分配给参数类型 'List<Series<dynamic, String>&g