image.png

解决思路

动态规划

image.png

  1. public int rob(int[] num){
  2. int preMax = 0;
  3. int currMax = 0;
  4. for (int i:num){
  5. int temp = currMax;
  6. currMax = Math.max(preMax + i,currMax);
  7. preMax = temp;
  8. }
  9. return currMax;
  10. }