There is a horizontal row of 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 is on top of then .
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 , the number of test cases.
For each test case, there are lines.
The first line of each test case contains , the number of cubes.
The second line contains space separated integers, denoting the sideLengths of each cube in that order.
For each test case, there are lines.
The first line of each test case contains , the number of cubes.
The second line contains space separated integers, denoting the sideLengths of each cube in that order.
Constraints
Output Format
For each test case, output a single line containing either "Yes" or "No" without the quotes.
Sample Input
2
6
4 3 2 1 3 4
3
1 3 2
Sample Output
Yes
No
Explanation
In the first test case, pick in this order: left - , right - , left - , right - , left - , right - .
In the second test case, no order gives an appropriate arrangement of vertical cubes. will always come after either or .
In the second test case, no order gives an appropriate arrangement of vertical cubes. will always come after either or .
Piling Up! - Hacker Rank Solution
In this problem we'll use a greedy approach and deque (Note that you can also use 2 pointers)
Keep a current variable then compare the largest element of both left and right of the main array and see if it's smaller than the current. This way you'll continue to add element in new array. See code below for implementation.
from collections import deque import sys for _ in range(input()): n = input() arr = map(int,raw_input().split()) for i in arr: assert i<=2**31 - 1 lst = deque(arr) curr = 9999999999999999 flag = 0 if (len(lst)<=2): print "Yes" continue left = lst.popleft() right = lst.pop() latest = -1 while (len(lst)>0): flag = 0 if (left >= right and left <= curr): curr = left left = lst.popleft() latest = left flag = 1 elif (right> left and right <= curr): curr = right right = lst.pop() latest = right flag = 1 if flag == 0: break if len(lst)>0 or latest > curr: print "No" else: print "Yes"
No comments:
Post a Comment