/** * @author : zhongxiankui */ public class DpLongestTest { public static int longest(int[] p, int[] r, int size){ if(r[size] >= 0){ return r[size]; } int q = -1; if(0 == size){ q = 0; }else { for(int i = 0; i < size; i++){ int v = longest(p, r, size - i - 1); q = q > (p[i] + v) ? q : (p[i] + v); } } r[size] = q; return q; } public static void test01(){ int[] p = new int[]{1, 5, 8, 9, 10, 17, 17, 20, 24, 30}; int size = p.length; int[] r = new int[size + 1]; for (int j = 0; j <= size; j++){ r[j] = -1; } longest(p, r, size); for (int j = 1; j <= size; j++){ System.out.println(j + ":" + r[j]); } } public static void main(String[] args){ test01(); } }
算法之动态规划
最新推荐文章于 2023-01-21 21:43:48 发布