Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

213. House Robber II

Posted on 2020-06-03 | Edited on 2021-01-22

LeetCode

DP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
private int robUtil(int[] nums,int start,int end) {
int prevMax=0,currMax=0;
for(int i=start;i<end;i++){
int tmp=currMax;
currMax=Math.max(prevMax+nums[i],currMax);
prevMax=tmp;
}
return currMax;
}
public int rob(int[] nums) {
if(nums.length==1) return nums[0];
return Math.max(robUtil(nums,0,nums.length-1),robUtil(nums,1,nums.length));
}
}

<1…182183184…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%