Skip to content

Commit

Permalink
Create house-robber.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Apr 1, 2015
1 parent a852298 commit 6fcc8dc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Python/house-robber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Time: O(n)
# Space: O(1)
#
# You are a professional robber planning to rob houses along a street.
# Each house has a certain amount of money stashed, the only constraint stopping you
# from robbing each of them is that adjacent houses have security system connected
# and it will automatically contact the police if two adjacent houses were broken into on the same night.
#
# Given a list of non-negative integers representing the amount of money of each house,
# determine the maximum amount of money you can rob tonight without alerting the police.
#
class Solution:
# @param num, a list of integer
# @return an integer
def rob(self, num):
if len(num) == 0:
return 0

if len(num) == 1:
return num[0]

num_i, num_i_1 = max(num[1], num[0]), num[0]
for i in xrange(2, len(num)):
num_i_1, num_i_2 = num_i, num_i_1
num_i = max(num[i] + num_i_2, num_i_1);

return num_i

if __name__ == '__main__':
print Solution().rob([8,4,8,5,9,6,5,4,4,10])

0 comments on commit 6fcc8dc

Please sign in to comment.