Simple-minded python solution (illustrative only)

  • 0

    This is my dumb verification function to make sure I understand the Josephus problem-like recursion. Hope it helps people that may come across it.


    def helper(self, x):
        # uncomment this to see the input
        # print(x)
        # trivial return
        if len(x)==1: return x
        # take every other element and then reverse the list
        y = x[1:len(x):2]
        # recurse
        return self.helper(y)
    def lastRemaining(self, n):
        # for illustrative purpose only,
        # manipulate the list until only one element remains
        nums = range(1,n+1)
        return (self.helper(nums)[0])


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.