这题核心思想是dynamic programming,我写了一个java代码,可以供大家思考public class Solution {
public static void main(String[] args){
int[] res = new int[50];
//base case
res[0] = 1;
res[1] = 1;int max = 0;for(int i = 2; i <50; i++) {//induction rule: when touch even number//check the difference at index i/2 and i/2-1//and use previous value(at index i-1) minus the difference,// which is cost when you chose the value at index i// and plus 1, which means you add the current valueif((i+1)%2==0){
res[i] = res[i-1]-(res[i/2]-res[i/2-1])+1;
}
else{
res[i] = res[i-1]+1;
}max = Math.max(max,res[i]);}
System.out.println(max);
}
}