Created
June 20, 2013 02:31
-
-
Save st0le/5819892 to your computer and use it in GitHub Desktop.
minhop dfs
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
def minhops_dfs(lst): | |
sz = len(lst) | |
min_hop = [0] * sz | |
def dfs(index,s,hops,min_hop): | |
if index >= sz: | |
if len(min_hop) > s: | |
min_hop[:] = hops | |
#min_hop.extend(hops) | |
elif s > len(min_hop): | |
pass | |
else: | |
hops.append(lst[index]) | |
for i in xrange(1,lst[index] + 1): | |
dfs(index + i, s + 1,hops,min_hop) | |
hops.pop() | |
dfs(0,0,[],min_hop) | |
return len(min_hop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment