Created
March 9, 2018 12:03
-
-
Save vincenttone/f2e754e62614cad0db2b5c9fde7bf70d to your computer and use it in GitHub Desktop.
你是一个盗窃专家,某一天晚上你要去盗窃某一条街道的一排房子。这些房子都有相连的防盗系统,如果你把相邻的两家都偷了那么就会触发报警器。 用一个数组来表示这些房子的金钱数量,请你完成 rob 函数,计算出在不触发报警器的情况下最多能偷多少钱。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def rob(x): | |
print 'rob: ', x | |
return most(x, 0, 2) | |
def most(x, n, r): | |
l = len(x) | |
pre = 0 | |
pre_keys = [] | |
for i in range(0, r): # nums need to count | |
k = n + i | |
if (l > k): | |
pre_val = most(x, k + r, r) | |
m1 = x[k] + pre_val[0] | |
keys = pre_val[1] | |
if m1 > pre: | |
pre = m1 | |
keys.append(str(k) + ':' + str(x[k])) | |
pre_keys = keys | |
return (pre, pre_keys) | |
print(rob([1,2,3])) | |
print(rob([9,7,3,4,1,8,1])) | |
print(rob([1,5,1,0,1,0,1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment