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 1901b6e commit cf08eceCopy full SHA for cf08ece
1 file changed
house-robber/se6816.java
@@ -0,0 +1,22 @@
1
+/**
2
+Problem 198 : House Robber
3
+Summary :
4
+- dp를 이용하여, 이전 결과를 저장하면서 문제를 해결한다.
5
+
6
+*/
7
8
+class Solution {
9
+ public int rob(int[] nums) {
10
+ int[] dp = new int[nums.length+1];
11
+ dp[1] = nums[0];
12
+ if(nums.length != 1) {
13
+ dp[2] = nums[1];
14
+ }
15
16
+ for(int i = 2; i < nums.length; i++) {
17
+ dp[i+1] = Math.max((nums[i] + dp[i-1]), (nums[i] + dp[i-2]));
18
19
20
+ return Math.max(dp[nums.length], dp[nums.length-1]);
21
22
+}
0 commit comments