Gaming Array 1

Sort by

recency

|

33 Discussions

|

  • + 0 comments

    O(n) solution using a stack:

    def gamingArray(arr):
        stack=[arr[0]]
        for a in arr:
            if a != stack[-1]:
                if a > stack[-1]:
                    stack.append(a)
    
        return "BOB" if len(stack) % 2 == 1 else "ANDY"
    
  • + 0 comments

    Python 3

    def gamingArray(arr):
        last = 1
        curr_max = arr[0]
        for number in arr[1:]:
            if number > curr_max:
                curr_max = number
                last += 1
        return "ANDY" if last % 2 == 0 else "BOB"
    
  • + 0 comments

    Here is my solution to handle large inputs in Java;

    private static TreeMap<Integer, Integer> indexMaxMap = new TreeMap<>();
        
        private static void createIndexMaxMap(List<Integer> arr) {
            indexMaxMap.clear();
            for (int i = 0; i < arr.size(); i++) {
                indexMaxMap.put(arr.get(i), i);
            }
        }
        
        private static List<Integer> getNewList(List<Integer> arr) {
            int lastIndex = arr.size() - 1;
            while(indexMaxMap.lastEntry().getValue() > lastIndex) {
                indexMaxMap.remove(indexMaxMap.lastKey());
            }
            
            int index = indexMaxMap.remove(indexMaxMap.lastKey());
            System.out.println("Last index: " + index);
            return arr.subList(0, index);
            
        }
        
        /*
         * Complete the 'gamingArray' function below.
         *
         * The function is expected to return a STRING.
         * The function accepts INTEGER_ARRAY arr as parameter.
         */
    
        public static String gamingArray(List<Integer> arr) {
            createIndexMaxMap(arr);
            int moveCount = 0;
            
            while (arr.size() > 0) {
                arr = getNewList(arr);
                moveCount++;
            }
            
            if (moveCount % 2 == 1) {
                return "BOB";
            }
            
            return "ANDY";
        }
    
  • + 0 comments

    Java, all test cases

     public static String gamingArray(List<Integer> arr) {
        int count = 0;
            int bigger = 0;
            
            for(Integer i : arr){
                if(i > bigger){
                    bigger = i;
                    count++;
                }    
            }
    
            if (count % 2 == 0) {
                return "ANDY";
            }
    
            return "BOB";
    }
    }
    
  • + 1 comment

    python3 slow version:

    def gamingArray(arr):
        number_of_moves = 0
        while len(arr) != 0:
            number_of_moves += 1
            arr = arr[:arr.index(max(arr))]
        if number_of_moves % 2 == 0:
            return "ANDY"
        return "BOB"
    

    fast version:

    def gamingArray(arr):
        number_of_moves = 0
        moves = [(0, arr[0])]
        for i, number in enumerate(arr[1:]):
            if number > moves[-1][1]:
                moves.append((i+1, number))
        if len(moves) % 2 == 0:
            return "ANDY"
        return "BOB"