Skip to content

Commit f43803c

Browse files
committed
Add house robber solution
1 parent 50a2ffb commit f43803c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

house-robber/hyunjung-choi.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode_study
2+
3+
class Solution {
4+
fun rob(nums: IntArray): Int {
5+
if (nums.size == 1) return nums[0]
6+
7+
var prev2 = nums[0]
8+
var prev1 = maxOf(nums[0], nums[1])
9+
10+
for (i in 2 until nums.size) {
11+
val current = maxOf(prev1, prev2 + nums[i])
12+
prev2 = prev1
13+
prev1 = current
14+
}
15+
16+
return prev1
17+
}
18+
}

0 commit comments

Comments
 (0)