Permutation game Discussions | | HackerRank

Permutation game

  • + 0 comments

    O(N^2) complexity

    def permutationGame(arr):
        memo = {}
    
        def winner(nums):
            if tuple(nums) in memo:
                return memo[tuple(nums)]
    
            if sorted(nums) == nums:
                memo[tuple(nums)] = 'Bob'
                return 'Bob'
    
            for i in range(len(nums)):
                if winner(nums[:i] + nums[i+1:]) == 'Bob':
                    memo[tuple(nums)] = 'Alice'
                    return 'Alice'
    
            memo[tuple(nums)] = 'Bob'
            return 'Bob'
    
        return winner(arr)