Permutation game Discussions | Algorithms | HackerRank
  • + 0 comments

    Python3 solution:

    from functools import cache
    def permutationGame(arr):
        def is_increasing(remain):
            return all(remain[i] < remain[i+1] for i in range(len(remain)-1))
        def new_perm(remain, removed):
            return tuple(e - 1 if e > removed else e for e in remain if e != removed)
        @cache
        def first_wins(remain):
            if is_increasing(remain):
                return False
            return any(not first_wins(new_perm(remain, e)) for e in remain)
        return 'Alice' if first_wins(tuple(arr)) else 'Bob'