• + 0 comments

    This is really nice looking code man, I really appreciated being able to work through your logic. This was my code by comparison...

    import math
    
    def is_stackable(cubes, bottom):
        while len(cubes) > 0:
            left = 0
            right = len(cubes) - 1
            # If we can't stack the cubes
            if cubes[left] > bottom or cubes[right] > bottom:
                return False
    
            # Check which cube to remove
            if cubes[left] > cubes[right]:
                bottom = cubes.pop(left)
            elif cubes[right] > cubes[left]:
                bottom = cubes.pop(right)
    
            # If we're on the last cube, check if it is stackable
            if len(cubes) == 1 and cubes[0] < bottom:
                return True
            elif len(cubes) == 1 and cubes[0] > bottom:
                return False
    
            # If the leftmost and rightmost cubes are the same
            if cubes[left] == cubes[right] and len(cubes) != 1:
                next_left = left
                next_right = right
                cube_to_remove = 0
    
                while cubes[next_left] == cubes[next_right]:
                    next_left += 1
                    next_right -= 1
    
                    # If the cubes are all equal up to the middle, then check if the
                    # middle is greater than any cube to the right or left
                    if next_left == next_right or next_right - next_left == 1:
                        for i in range(next_right, len(cubes) - 1):
                            if cubes[next_right] > cubes[i]:
                                return False
                        for i in range(0, next_left):
                            if cubes[next_left] > cubes[i]:
                                return False
                        # If the middle cubes aren't bigger than any of the outer cubes, return True
                        return True
    
                    # If the inner cubes are also the same, skip to the next iteration
                    if cubes[next_left] == cubes[next_right]:
                        continue
    
                    # Identify which side we will draw from
                    if cubes[next_left] > cubes[next_right]:
                        cube_to_remove = 0
                    elif cubes[next_right] > cubes[next_left]:
                        cube_to_remove = len(cubes) - 1
    
                # Remove the cube from the row
                bottom = cubes.pop(cube_to_remove)
                continue
    
    num_test_cases = int(input())
    test_cases = list()
    
    for _ in range(num_test_cases):
        case = list()
        case.append(int(input()))
        case.append(list(map(int, input().split())))
        test_cases.append(case)
    
    for case in test_cases:
        bottom = math.inf
        num_cubes = case[0]
        cubes = case[1]
    
        if is_stackable(cubes, bottom):
            print('Yes')
        else:
            print('No')