严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

House RobberII 高频题

Go down

House RobberII 高频题 Empty House RobberII 高频题

Post by Admin Sat Oct 21, 2017 4:59 pm

public int rob(int[] nums) {
int n = nums.length;
if (n == 0) return 0;
if (n == 1) return nums[0];
return Math.max(robBetween(nums, 0, n - 2), robBetween(nums, 1, n - 1));
}

public int robBetween(int[] nums, int start, int end) {
int prepre = 0;
int pre = nums[start];
for (int i = start + 1; i <= end; i++) {
int cur = Math.max(prepre + nums[i], pre);
prepre = pre;
pre = cur;
}
return pre;
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum