We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d2d0e7d commit 4409730Copy full SHA for 4409730
house-robber/forest000014.java
@@ -0,0 +1,23 @@
1
+/*
2
+Runtime: 0 ms(Beats: 100.00 %)
3
+Time Complexity: O(nlogn)
4
+- nums iteration : O(n)
5
+
6
+Memory: 41.40 MB(Beats: 43.05 %)
7
+Space Complexity: O(n)
8
+- dp[n][2] : O(n) * 2 = O(n)
9
+*/
10
11
+class Solution {
12
+ public int rob(int[] nums) {
13
+ int[][] dp = new int[nums.length][2];
14
15
+ dp[0][1] = nums[0];
16
+ for (int i = 1; i < nums.length; i++) {
17
+ dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
18
+ dp[i][1] = dp[i - 1][0] + nums[i];
19
+ }
20
21
+ return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]);
22
23
+}
0 commit comments