=====Problem Statement=====
There is a horizontal row of n cubes. The length of each cube is given. You need to create a new vertical pile of cubes. The new pile should follow these directions: if cube_i is on top of cube_j then sideLength_j ≥ sideLength_i.
When stacking the cubes, you can only pick up either the leftmost or the rightmost cube each time. Print "Yes" if it is possible to stack the cubes. Otherwise, print "No". Do not print the quotation marks. 

=====Input Format=====
The first line contains a single integer T, the number of test cases.
For each test case, there are 2 lines.
The first line of each test case contains n, the number of cubes.
The second line contains n space separated integers, denoting the sideLengths of each cube in that order. 

=====Constraints=====
1≤T≤5
1≤n≤10^5
1≤sideLength≤2^31

=====Output Format=====
For each test case, output a single line containing either "Yes" or "No" without the quotes.

Difficulty: introductory

Starter code:

# Enter your code here. Read input from STDIN. Print output to STDOUT

Solutions:
from collections import deque
cas = int(input())
for t in range(cas):
    n = int(input())
    dq = deque(list(map(int,input().split())))
    possible = True
    element = (2**31)+1
    while dq:
        left_element = dq[0]
        right_element = dq[-1]
        if left_element>=right_element and element>=left_element:
            element = dq.popleft()
        elif right_element>=left_element and element>=right_element:
            element = dq.pop()
        else:
            possible = False
            break
    if possible:
        print('Yes')
    else:
        print('No')   


#!/usr/bin/env python3

from collections import deque

def __starting_point():
    t = int(input().strip())
    
    for _ in range(t):
        num_cnt = int(input().strip())
        deq = deque(list(map(int, input().strip().split(' '))))
        
        prev = max(deq[0], deq[-1])
        while deq:
            if prev >= deq[0] and prev >= deq[-1]:
                if deq[0] >= deq[-1]:
                    prev = deq.popleft()
                else:
                    prev = deq.pop()
            else:
                break
                
        if len(deq) == 0:
            print('Yes')
        else:
            print('No')
            

__starting_point()