Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e80ccfe

Browse files
authored
Create 198-House-Robber.rb
1 parent e86e2dc commit e80ccfe

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ruby/198-House-Robber.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @param {Integer[]} nums
2+
# @return {Integer}
3+
def rob(nums)
4+
dp = Array.new(nums.size + 3) { 0 }
5+
6+
nums.each_with_index do |num, i|
7+
dp[i+3] = num + [dp[i+1], dp[i]].max
8+
end
9+
10+
[dp[-1], dp[-2]].max
11+
end

0 commit comments

Comments
 (0)