Skip to content

Commit 33b1096

Browse files
committed
Solution House Robber
1 parent cdd9f29 commit 33b1096

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

house-robber/doitduri.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func rob(_ nums: [Int]) -> Int {
3+
let length = nums.count
4+
5+
if length == 1 {
6+
return nums[0]
7+
}
8+
9+
if length == 2 {
10+
return max(nums[0], nums[1])
11+
}
12+
13+
var tables = Array(repeating: 0, count: length)
14+
tables[0] = nums[0]
15+
tables[1] = max(nums[0], nums[1])
16+
17+
for i in 2..<length {
18+
tables[i] = max(nums[i]+tables[i-2], tables[i-1])
19+
}
20+
21+
return tables[length-1]
22+
}
23+
}

0 commit comments

Comments
 (0)