SHARE
TWEET

Untitled

a guest Aug 21st, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     public int rob(int[] nums) {
  3.         if (nums.length == 0) return 0;
  4.         if (nums.length == 1) return nums[0];
  5.         int n = nums.length;
  6.         int[] rob = new int[n];
  7.         rob[0] = nums[0];
  8.         rob[1] = Math.max(nums[0], nums[1]);
  9.         for(int i = 2; i < n; i++) {
  10.             rob[i] = Math.max(rob[i-1], rob[i-2] + nums[i]);
  11.         }
  12.         return rob[n-1];
  13.     }
  14. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top