Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
31 lines (22 loc) · 812 Bytes

File metadata and controls

31 lines (22 loc) · 812 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Solution {
public int rob(int[] num) {
int le = num.length;
if(le==0) return 0;
else if (le==1) return num[0];
else if (le==2) return Math.max(num[0],num[1]);
int[][] dp = new int[num.length + 1][2];
for (int i = 1; i <= num.length-1; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = num[i - 1] + dp[i - 1][0];
}
int ret1 = Math.max(dp[num.length-1][0], dp[num.length-1][1]);
int[][] dp2 = new int[num.length + 1][2];
for (int i = 2; i <= num.length; i++) {
dp2[i][0] = Math.max(dp2[i - 1][0], dp2[i - 1][1]);
dp2[i][1] = num[i - 1] + dp2[i - 1][0];
}
int ret2 = Math.max(dp2[num.length][0], dp2[num.length][1]);
return Math.max(ret1, ret2);
}
}
//[2 1 1 1]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.