Permutation game Discussions | | HackerRank

Permutation game

Sort by

recency

|

15 Discussions

|

  • + 0 comments

    Recursion with memorization solution:

    def permutationGame(arr):
        memo = {}
        def canWin(subArray):
            if tuple(subArray) in memo:
                return memo[tuple(subArray)]
                
            if subArray == sorted(subArray):
                memo[tuple(subArray)] = False
                return False
            
            for i in range(len(subArray)):
                newArray = subArray[:i] + subArray[i+1:]
                if not canWin(newArray):
                    memo[tuple(subArray)] = True
                    return True
            memo[tuple(subArray)] = False
            return False
            
        return "Alice" if canWin(arr) else "Bob"
    
  • + 0 comments

    @judge_angry solution:

    static Map<String, Boolean> memo = new HashMap<>();
    
    public static String permutationGame(List<Integer> arr) {
    // Write your code here
    memo.clear();
    return findWinner(arr) ? "Bob" : "Alice";
    
    }
    
    static boolean isIncreasing(List<Integer> arr) {
        for (int i = 0; i < arr.size() - 1; i++) {
            if (arr.get(i) > arr.get(i + 1)) {
                return false;
        }
    }
    return true;
    }
    
    static boolean findWinner(List<Integer> arr) {
        String key = arr.toString();
        if (memo.containsKey(key)) {
            return memo.get(key);
        }
        if (isIncreasing(arr)) {
            memo.put(key, true);
            return true;
        }
        for (int i = 0; i < arr.size(); i++) {
            if (findWinner(Stream.concat(arr.subList(0, i).stream(),
                arr.subList(i+1, arr.size()).stream())
                .collect(Collectors.toList()))) {
            memo.put(key, false);
            return false;
            }
        }
        memo.put(key, true);
        return true;
    }
    
  • + 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"
    
  • + 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)
    
  • + 0 comments

    Python

    @functools.lru_cache(None)
    def PG(arr):
        if list(arr) == sorted(list(arr)):
            return 'Bob' 
        else:
            for i in range(len(arr)):
                if PG(arr[:i]+arr[i+1:]) == 'Bob': 
                    return 'Alice'
            return 'Bob'   
        
    def permutationGame(arr):
        return PG(tuple(arr))