I was working on this problem where I need to remove items from the list from left to right and then right to left till only one item remains. To solve this I was using the recursion method and I was able to get the desired values. But for some reason, the return statement won't work when the if the condition(len(addL) == 1) is True. Appreciate any help on this. Please check the code below
class Solution(object):
def lastRemaining(self, n):
addL = []
switch = False
for i in xrange(1,n + 1):
addL.append(i)
def removeO(addL,switch):
if len(addL) == 1:
return addL
if len(addL) != 1:
if not switch:
addL = [item for item in addL if item not in addL[::2]]
switch = True
removeO(addL,switch)
if switch:
addL = [item for item in addL if item not in addL[::-2]]
switch = False
removeO(addL,switch)
final = removeO(addL,False)
return final
Aucun commentaire:
Enregistrer un commentaire