class Solution { public: /** * @param nums: An array of non-negative integers. * @return: The maximum amount of money you can rob tonight */ int houseRobber(vector<int> &A) { // write your code here int n = A.size(); if (n == 0) return 0; if (n == 1) return A[0]; if (n == 2) return max(A[0], A[1]); vector<int> res(n, 0); res[n-1] = A[n-1]; res[n-2] = max(A[n-1], A[n-2]); for (int i = n-3; i >= 0; i--) res[i] = max(A[i] + res[i+2], res[i+1]); return res[0]; }
int houseRobber2(vector<int> &nums) { // write your code here if (nums.size() == 1) return nums[0]; vector<int> temp1 = nums, temp2 = nums; temp1[0] = 0; temp2[nums.size() - 1] = 0; return max(houseRobber(temp1),houseRobber(temp2)); } };
-------------end of file
thanks for reading-------------