-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRob.java
43 lines (42 loc) · 1.28 KB
/
Rob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package base.dp;
/**
* @program: LeetCode
* @description: LeetCode198.打家劫舍
* @author: Mr.Zhangmy
* @create: 2020-08-07 17:49
* ****************************************************
* url: https://leetcode-cn.com/problems/house-robber/
* ****************************************************
* 解题思路:
* 可偷窃的户: nums[]
* 最大偷窃金额: maxProfit[]
* 1 : maxProfit[0]=nums[0]
* 2 : maxProfit[1]=max(maxProfit[0],nums[1])
* 3 : maxProfit[2]=max(maxProfit[0]+nums[2],maxProfit[1])
* n : maxProfit[n-1]=max(maxProfit[n-3]+nums[n-1],maxProfit[n-2])
**/
public class Rob {
public int rob(int[] nums) {
int len = nums.length;
if (len<1){
return 0;
}else if (len==1){
return nums[0];
}
int[] maxProfit = new int[len];
maxProfit[0]=nums[0];
maxProfit[1]=nums[0]>nums[1]?nums[0]:nums[1];
for (int i=2;i<len;i++){
maxProfit[i]=maxProfit[i-2]+nums[i]>maxProfit[i-1]?maxProfit[i-2]+nums[i]:maxProfit[i-1];
}
int maxValue = getMaxValue(maxProfit);
return maxValue;
}
public static int getMaxValue(int array[]){
int max=array[0];
for (int tmp: array) {
max=max>=tmp?max:tmp;
}
return max;
}
}