Advertisement
djphrancis

Piling Up

Jul 28th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. from collections import deque
  2.  
  3. def can_be_piled(cubes: list) -> bool:
  4.     cube_row = deque(map(int, cubes))
  5.     pile = []
  6.     while len(cube_row) > 0:
  7.         # select the bigger of the left or right cubes
  8.         if cube_row[0] >= cube_row[-1]:
  9.             cube = cube_row.popleft()
  10.         else:
  11.             cube = cube_row.pop()
  12.         # handle first cube
  13.         if len(pile) == 0:
  14.             pile.append(cube)
  15.         # handle subsequent cubes
  16.         else:
  17.             if pile[-1] >= cube:
  18.                 pile.append(cube)
  19.             else:
  20.                 return False
  21.     return True
  22.            
  23.  
  24. if __name__ == "__main__":
  25.     num_cases = int(input())
  26.     for _ in range(num_cases):
  27.         cube_row = deque()
  28.         num_cubes = int(input())
  29.         if can_be_piled(input().split()):
  30.             print('Yes')
  31.         else:
  32.             print('No')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement