参考:https://www.bilibili.com/video/BV12W411v7rd
import java.util.Arrays; public class OptMain { public static void main(String[] args) { int[] arr = {4,1,1,9,1,10}; dp_opt(arr); } private static void dp_opt(int[] arr){ int[] temp = new int[arr.length]; for (int i = 0; i < arr.length; i++) { if(i == 0){ temp[0] = arr[0]; } else if(i == 1){ temp[1] = Math.max(arr[0],arr[1]); } else { temp[i] = Math.max(temp[i-2] + arr[i],temp[i-1]); } } System.out.println(Arrays.toString(temp)); } }