Permutation game Discussions | | HackerRank

Permutation game

  • + 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;
    }