Skip to content

Commit 6fcc8dc

Browse files
committed
Create house-robber.py
1 parent a852298 commit 6fcc8dc

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Python/house-robber.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
#
4+
# You are a professional robber planning to rob houses along a street.
5+
# Each house has a certain amount of money stashed, the only constraint stopping you
6+
# from robbing each of them is that adjacent houses have security system connected
7+
# and it will automatically contact the police if two adjacent houses were broken into on the same night.
8+
#
9+
# Given a list of non-negative integers representing the amount of money of each house,
10+
# determine the maximum amount of money you can rob tonight without alerting the police.
11+
#
12+
class Solution:
13+
# @param num, a list of integer
14+
# @return an integer
15+
def rob(self, num):
16+
if len(num) == 0:
17+
return 0
18+
19+
if len(num) == 1:
20+
return num[0]
21+
22+
num_i, num_i_1 = max(num[1], num[0]), num[0]
23+
for i in xrange(2, len(num)):
24+
num_i_1, num_i_2 = num_i, num_i_1
25+
num_i = max(num[i] + num_i_2, num_i_1);
26+
27+
return num_i
28+
29+
if __name__ == '__main__':
30+
print Solution().rob([8,4,8,5,9,6,5,4,4,10])

0 commit comments

Comments
 (0)