Ideological renewal equation DP also thought of himself, but not a complete write.
Recurrence equation: dp [i] = Math.max ( dp [i - 1], dp [i - 2] + nums [i]);
Author: sweetiee
link: https: //leetcode-cn.com/problems/the-masseuse-lcci/solution/100-kong-jian-cong-onyou-hua-dao-o1bao-hui-by-swee/
two way , the second difficult to understand but more concise.
public int massage(int[] nums) {
if (nums == null || nums.length ==0){
return 0;
}
if (nums.length == 1){
return nums[0];
}
int[] res = new int[nums.length];
res[0] = nums[0];
res[1] = Math.max(nums[0],nums[1]);
for (int i = 2; i < nums.length; ++i) {
res[i] = Math.max(res[i - 1],res[i - 2] + nums[i]);
}
return res[nums.length - 1];
}
class Solution {
public int massage(int[] nums) {
int a = 0, b = 0;
for (int i = 0; i < nums.length; i++) {
int c = Math.max(b, a + nums[i]);
a = b;
b = c;
}
return b;
}
}