Gaming Array 1

Sort by

recency

|

36 Discussions

|

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn gaming_array(arr: &[i32]) -> String {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(1)
        let mut max_number = arr[0];
        let mut turns = 0;
        for &number in arr {
            if number > max_number {
                max_number = number;
                turns += 1
            }
        }
        if turns % 2 == 0 {
            String::from("BOB")
        } else {
            String::from("ANDY")
        }
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def gaming_array(arr):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(1)
        max_number = arr[0]
        turns = 0
        for number in arr:
            if number > max_number:
                max_number = number
                turns += 1
        if turns % 2 == 0:
            return "BOB"
        else:
            return "ANDY"
    
  • + 0 comments
    #Python
    def gamingArray(arr):
        cuts = 0
        current = 0
        
        for val in arr:
            if val > current: 
                cuts += 1
                current = val
        
        return "ANDY" if cuts % 2 == 0 else "BOB"
    
  • + 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"