Frist use recursive methods,then use HashMapto cache results(DP)