Permutation game Discussions | | HackerRank

Permutation game

  • + 0 comments

    Memoized top-down solution worked, for my surprise:

    def willWinPermutationGame(tup, memo):
        if len(tup) == 1:
            return False
        
        if memo.get(tup) is not None:
            return memo[tup]
        
        is_increasing = True
        pivot = tup[0]
        for i in range(1, len(tup)):
            if tup[i] < pivot:
                is_increasing = False
                break
            pivot = tup[i]
        if is_increasing:
            memo[tup] = False
            return False
        
        for i in range(len(tup)):
            left = [] if i == 0 else list(tup[:i])
            right = [] if i == len(tup) - 1 else list(tup[i+1:])
            if not willWinPermutationGame(tuple(left + right), memo):
                memo[tup] = True
                return True
        
        return False
        
    
    def permutationGame(arr):
        memo = {}
        return "Alice" if willWinPermutationGame(tuple(arr), memo) else "Bob"